|
| 1 | +import type {JsonObject} from 'type-fest'; |
| 2 | + |
| 3 | +/** |
| 4 | +Exposed for `instanceof` checking. |
| 5 | +*/ |
| 6 | +export type JSONError = Error & { // eslint-disable-line @typescript-eslint/naming-convention |
| 7 | + /** |
| 8 | + The filename displayed in the error message, if any. |
| 9 | + */ |
| 10 | + fileName: string; |
| 11 | + |
| 12 | + /** |
| 13 | + The printable section of the JSON which produces the error. |
| 14 | + */ |
| 15 | + readonly codeFrame: string; |
| 16 | +}; |
| 17 | + |
| 18 | +// Get 'reviver' parameter from JSON.parse() |
| 19 | +type ReviverFn = Parameters<typeof JSON['parse']>['1']; |
| 20 | + |
| 21 | +/** |
| 22 | +Parse JSON with more helpful errors. |
| 23 | +
|
| 24 | +@param string - A valid JSON string. |
| 25 | +@param reviver - Prescribes how the value originally produced by parsing is transformed, before being returned. See [`JSON.parse` docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter |
| 26 | +) for more. |
| 27 | +@param filename - The filename displayed in the error message. |
| 28 | +@returns A parsed JSON object. |
| 29 | +@throws A {@link JSONError} when there is a parsing error. |
| 30 | +
|
| 31 | +@example |
| 32 | +``` |
| 33 | +import parseJson, {JSONError} from 'parse-json'; |
| 34 | +
|
| 35 | +const json = '{\n\t"foo": true,\n}'; |
| 36 | +
|
| 37 | +parseJson(json); |
| 38 | +// JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' |
| 39 | +// |
| 40 | +// 1 | { |
| 41 | +// 2 | "foo": true, |
| 42 | +// > 3 | } |
| 43 | +// | ^ |
| 44 | +
|
| 45 | +parseJson(json, 'foo.json'); |
| 46 | +// JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json |
| 47 | +// |
| 48 | +// 1 | { |
| 49 | +// 2 | "foo": true, |
| 50 | +// > 3 | } |
| 51 | +// | ^ |
| 52 | +
|
| 53 | +// You can also add the filename at a later point |
| 54 | +try { |
| 55 | + parseJson(json); |
| 56 | +} catch (error) { |
| 57 | + if (error instanceof JSONError) { |
| 58 | + error.fileName = 'foo.json'; |
| 59 | + } |
| 60 | +
|
| 61 | + throw error; |
| 62 | +} |
| 63 | +// JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json |
| 64 | +// |
| 65 | +// 1 | { |
| 66 | +// 2 | "foo": true, |
| 67 | +// > 3 | } |
| 68 | +// | ^ |
| 69 | +``` |
| 70 | +*/ |
| 71 | +export default function parseJson(string: string, reviver?: ReviverFn, filename?: string): JsonObject; |
| 72 | + |
| 73 | +/** |
| 74 | +Parse JSON with more helpful errors. |
| 75 | +
|
| 76 | +@param string - A valid JSON string. |
| 77 | +@param filename - The filename displayed in the error message. |
| 78 | +@returns A parsed JSON object. |
| 79 | +@throws A {@link JSONError} when there is a parsing error. |
| 80 | +
|
| 81 | +@example |
| 82 | +``` |
| 83 | +import parseJson, {JSONError} from 'parse-json'; |
| 84 | +
|
| 85 | +const json = '{\n\t"foo": true,\n}'; |
| 86 | +
|
| 87 | +parseJson(json); |
| 88 | +// JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' |
| 89 | +// |
| 90 | +// 1 | { |
| 91 | +// 2 | "foo": true, |
| 92 | +// > 3 | } |
| 93 | +// | ^ |
| 94 | +
|
| 95 | +parseJson(json, 'foo.json'); |
| 96 | +// JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json |
| 97 | +// |
| 98 | +// 1 | { |
| 99 | +// 2 | "foo": true, |
| 100 | +// > 3 | } |
| 101 | +// | ^ |
| 102 | +
|
| 103 | +// You can also add the filename at a later point |
| 104 | +try { |
| 105 | + parseJson(json); |
| 106 | +} catch (error) { |
| 107 | + if (error instanceof JSONError) { |
| 108 | + error.fileName = 'foo.json'; |
| 109 | + } |
| 110 | +
|
| 111 | + throw error; |
| 112 | +} |
| 113 | +// JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json |
| 114 | +// |
| 115 | +// 1 | { |
| 116 | +// 2 | "foo": true, |
| 117 | +// > 3 | } |
| 118 | +// | ^ |
| 119 | +``` |
| 120 | +*/ |
| 121 | +export default function parseJson(string: string, filename?: string): JsonObject; |
0 commit comments