frontend/src/lib/json.ts
raw| 1 | import type { Result } from "./result"; |
| 2 | import { err, ok } from "./result"; |
| 3 | |
| 4 | /** |
| 5 | * Parse a JSON string into an object. |
| 6 | */ |
| 7 | export function parseJSON(data: string): Result<unknown, SyntaxError> { |
| 8 | try { |
| 9 | return ok(JSON.parse(data)); |
| 10 | } catch (error) { |
| 11 | if (error instanceof SyntaxError) { |
| 12 | return err(error); |
| 13 | } |
| 14 | throw error; |
| 15 | } |
| 16 | } |