UNPKG

3.15 kBTypeScriptView Raw
1import type {JsonObject} from 'type-fest';
2
3/**
4Exposed for `instanceof` checking.
5*/
6export class JSONError extends 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 The raw version of `codeFrame` without colors.
19 */
20 readonly rawCodeFrame: string;
21}
22
23// Get `reviver`` parameter from `JSON.parse()`.
24export type Reviver = Parameters<typeof JSON['parse']>['1'];
25
26/**
27Parse JSON with more helpful errors.
28
29@param string - A valid JSON string.
30@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
31) for more.
32@param filename - The filename displayed in the error message.
33@returns A parsed JSON object.
34@throws A {@link JSONError} when there is a parsing error.
35
36@example
37```
38import parseJson, {JSONError} from 'parse-json';
39
40const json = '{\n\t"foo": true,\n}';
41
42parseJson(json);
43// JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}'
44//
45// 1 | {
46// 2 | "foo": true,
47// > 3 | }
48// | ^
49
50parseJson(json, 'foo.json');
51// JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json
52//
53// 1 | {
54// 2 | "foo": true,
55// > 3 | }
56// | ^
57
58// You can also add the filename at a later point
59try {
60 parseJson(json);
61} catch (error) {
62 if (error instanceof JSONError) {
63 error.fileName = 'foo.json';
64 }
65
66 throw error;
67}
68// JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json
69//
70// 1 | {
71// 2 | "foo": true,
72// > 3 | }
73// | ^
74```
75*/
76export default function parseJson(string: string, reviver?: Reviver, filename?: string): JsonObject;
77
78/**
79Parse JSON with more helpful errors.
80
81@param string - A valid JSON string.
82@param filename - The filename displayed in the error message.
83@returns A parsed JSON object.
84@throws A {@link JSONError} when there is a parsing error.
85
86@example
87```
88import parseJson, {JSONError} from 'parse-json';
89
90const json = '{\n\t"foo": true,\n}';
91
92parseJson(json);
93// JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}'
94//
95// 1 | {
96// 2 | "foo": true,
97// > 3 | }
98// | ^
99
100parseJson(json, 'foo.json');
101// JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json
102//
103// 1 | {
104// 2 | "foo": true,
105// > 3 | }
106// | ^
107
108// You can also add the filename at a later point
109try {
110 parseJson(json);
111} catch (error) {
112 if (error instanceof JSONError) {
113 error.fileName = 'foo.json';
114 }
115
116 throw error;
117}
118// JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json
119//
120// 1 | {
121// 2 | "foo": true,
122// > 3 | }
123// | ^
124```
125*/
126export default function parseJson(string: string, filename?: string): JsonObject;