Skip to content

Commit 67144b9

Browse files
alan-agius4clydin
authored andcommitted
refactor(@angular-devkit/core): remove deprecated parseJson and ParseJsonOptions APIs
BREAKING CHANGE: `parseJson` and `ParseJsonOptions` APIs have been removed in favor of 3rd party JSON parsers such as `jsonc-parser`.
1 parent d163229 commit 67144b9

File tree

2 files changed

+3
-133
lines changed

2 files changed

+3
-133
lines changed

packages/angular_devkit/core/src/json/parser.ts

+1-59
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
JsonAstString,
2424
Position,
2525
} from './parser_ast';
26-
import { JsonArray, JsonObject, JsonValue } from './utils';
26+
import { JsonArray, JsonObject } from './utils';
2727

2828
export class JsonException extends BaseException {}
2929

@@ -61,16 +61,6 @@ export class UnexpectedEndOfInputException extends JsonException {
6161
}
6262
}
6363

64-
/**
65-
* An error happened within a file.
66-
* @deprecated Deprecated since version 11. Use 3rd party JSON parsers such as `jsonc-parser` instead.
67-
*/
68-
export class PathSpecificJsonException extends JsonException {
69-
constructor(public path: string, public exception: JsonException) {
70-
super(`An error happened at file path ${JSON.stringify(path)}: ${exception.message}`);
71-
}
72-
}
73-
7464
/**
7565
* Context passed around the parser with information about where we currently are in the parse.
7666
* @deprecated Deprecated since version 11. Use 3rd party JSON parsers such as `jsonc-parser` instead.
@@ -904,51 +894,3 @@ export function parseJsonAst(input: string, mode = JsonParseMode.Default): JsonA
904894

905895
return ast;
906896
}
907-
908-
/**
909-
* Options for the parseJson() function.
910-
* @deprecated Deprecated since version 11. Use 3rd party JSON parsers such as `jsonc-parser` instead.
911-
*/
912-
export interface ParseJsonOptions {
913-
/**
914-
* If omitted, will only emit errors related to the content of the JSON. If specified, any
915-
* JSON errors will also include the path of the file that caused the error.
916-
*/
917-
path?: string;
918-
}
919-
920-
/**
921-
* Parse a JSON string into its value. This discards the AST and only returns the value itself.
922-
*
923-
* If a path option is pass, it also absorbs JSON parsing errors and return a new error with the
924-
* path in it. Useful for showing errors when parsing from a file.
925-
*
926-
* @deprecated Deprecated since version 11. Use 3rd party JSON parsers such as `jsonc-parser` instead.
927-
* @param input The string to parse.
928-
* @param mode The mode to parse the input with. {@see JsonParseMode}.
929-
* @param options Additional optinos for parsing.
930-
* @returns {JsonValue} The value represented by the JSON string.
931-
*/
932-
export function parseJson(
933-
input: string,
934-
mode = JsonParseMode.Default,
935-
options?: ParseJsonOptions,
936-
): JsonValue {
937-
try {
938-
// Try parsing for the fastest path available, if error, uses our own parser for better errors.
939-
if (mode == JsonParseMode.Strict) {
940-
try {
941-
return JSON.parse(input);
942-
} catch (err) {
943-
return parseJsonAst(input, mode).value;
944-
}
945-
}
946-
947-
return parseJsonAst(input, mode).value;
948-
} catch (e) {
949-
if (options && options.path && e instanceof JsonException) {
950-
throw new PathSpecificJsonException(options.path, e);
951-
}
952-
throw e;
953-
}
954-
}

packages/angular_devkit/core/src/json/parser_spec.ts

+2-74
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { JsonParseMode, parseJson, parseJsonAst } from './parser';
9+
import { JsonParseMode, parseJsonAst } from './parser';
1010

1111
// Node 6 compatibility.
1212
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1313
function entries(x: { [key: string]: any }): any {
1414
return Object.keys(x).map((k) => [k, x[k]]);
1515
}
1616

17-
describe('parseJson and parseJsonAst', () => {
17+
describe('parseJsonAst', () => {
1818
describe('generic', () => {
1919
const numbers = {};
2020
const errors = ['', '-abcdefghijklmnopqrstuvwxyz'];
@@ -25,15 +25,13 @@ describe('parseJson and parseJsonAst', () => {
2525
expect(ast.start).toEqual({ offset: start[0], line: start[1], character: start[2] });
2626
expect(ast.end).toEqual({ offset: end[0], line: end[1], character: end[2] });
2727
expect(ast.value).toEqual(JSON.parse(n));
28-
expect(parseJson(n)).toEqual(JSON.parse(n));
2928
expect(ast.text).toBe(text === undefined ? n : text);
3029
});
3130
}
3231

3332
for (const n of errors) {
3433
it(`errors for ${JSON.stringify(n)}`, () => {
3534
expect(() => parseJsonAst(n)).toThrow();
36-
expect(() => parseJson(n)).toThrow();
3735
expect(() => JSON.parse(n)).toThrow();
3836
});
3937
}
@@ -98,15 +96,13 @@ describe('parseJson and parseJsonAst', () => {
9896
expect(ast.start).toEqual({ offset: start[0], line: start[1], character: start[2] });
9997
expect(ast.end).toEqual({ offset: end[0], line: end[1], character: end[2] });
10098
expect(ast.value).toEqual(JSON.parse(n));
101-
expect(parseJson(n)).toEqual(JSON.parse(n));
10299
expect(ast.text).toBe(text === undefined ? n : text);
103100
});
104101
}
105102

106103
for (const n of errors) {
107104
it(`errors for ${JSON.stringify(n)}`, () => {
108105
expect(() => parseJsonAst(n)).toThrow();
109-
expect(() => parseJson(n)).toThrow();
110106
expect(() => JSON.parse(n)).toThrow();
111107
});
112108
}
@@ -144,15 +140,13 @@ describe('parseJson and parseJsonAst', () => {
144140
expect(ast.start).toEqual({ offset: start[0], line: start[1], character: start[2] });
145141
expect(ast.end).toEqual({ offset: end[0], line: end[1], character: end[2] });
146142
expect(ast.value).toEqual(JSON.parse(n));
147-
expect(parseJson(n)).toEqual(JSON.parse(n));
148143
expect(ast.text).toBe(text === undefined ? n : text);
149144
});
150145
}
151146

152147
for (const n of errors) {
153148
it(`errors for ${JSON.stringify(n)}`, () => {
154149
expect(() => parseJsonAst(n)).toThrow();
155-
expect(() => parseJson(n)).toThrow();
156150
expect(() => JSON.parse(n)).toThrow();
157151
});
158152
}
@@ -180,7 +174,6 @@ describe('parseJson and parseJsonAst', () => {
180174
for (const n of errors) {
181175
it(`errors for ${JSON.stringify(n)}`, () => {
182176
expect(() => parseJsonAst(n)).toThrow();
183-
expect(() => parseJson(n)).toThrow();
184177
expect(() => JSON.parse(n)).toThrow();
185178
});
186179
}
@@ -223,15 +216,13 @@ describe('parseJson and parseJsonAst', () => {
223216
expect(ast.start).toEqual({ offset: start[0], line: start[1], character: start[2] });
224217
expect(ast.end).toEqual({ offset: end[0], line: end[1], character: end[2] });
225218
expect(ast.value).toEqual(JSON.parse(n));
226-
expect(parseJson(n)).toEqual(JSON.parse(n));
227219
expect(ast.text).toBe(text === undefined ? n : text);
228220
});
229221
}
230222

231223
for (const n of errors) {
232224
it(`errors for ${JSON.stringify(n)}`, () => {
233225
expect(() => parseJsonAst(n)).toThrow();
234-
expect(() => parseJson(n)).toThrow();
235226
expect(() => JSON.parse(n)).toThrow();
236227
});
237228
}
@@ -269,15 +260,13 @@ describe('parseJson and parseJsonAst', () => {
269260
expect(ast.start).toEqual({ offset: start[0], line: start[1], character: start[2] });
270261
expect(ast.end).toEqual({ offset: end[0], line: end[1], character: end[2] });
271262
expect(ast.value).toEqual(JSON.parse(n));
272-
expect(parseJson(n)).toEqual(JSON.parse(n));
273263
expect(ast.text).toBe(text === undefined ? n : text);
274264
});
275265
}
276266

277267
for (const n of errors) {
278268
it(`errors for ${JSON.stringify(n)}`, () => {
279269
expect(() => parseJsonAst(n)).toThrow();
280-
expect(() => parseJson(n)).toThrow();
281270
expect(() => JSON.parse(n)).toThrow();
282271
});
283272
}
@@ -322,76 +311,15 @@ describe('parseJson and parseJsonAst', () => {
322311
expect(ast.start).toEqual({ offset: start[0], line: start[1], character: start[2] });
323312
expect(ast.end).toEqual({ offset: end[0], line: end[1], character: end[2] });
324313
expect(ast.value).toEqual(value);
325-
expect(parseJson(n, JsonParseMode.Loose)).toEqual(value);
326314
expect(ast.text).toBe(text === undefined ? n : text);
327315
});
328316
}
329317

330318
for (const n of errors) {
331319
it(`errors for ${JSON.stringify(n)}`, () => {
332320
expect(() => parseJsonAst(n, JsonParseMode.Loose)).toThrow();
333-
expect(() => parseJson(n, JsonParseMode.Loose)).toThrow();
334321
expect(() => JSON.parse(n)).toThrow();
335322
});
336323
}
337324
});
338-
339-
describe('complex', () => {
340-
it('strips comments', () => {
341-
expect(
342-
parseJson(
343-
`
344-
// THIS IS A COMMENT
345-
{
346-
/* THIS IS ALSO A COMMENT */ // IGNORED BECAUSE COMMENT
347-
// AGAIN, COMMENT /* THIS SHOULD NOT BE WEIRD
348-
"a": "this // should not be a comment",
349-
"a2": "this /* should also not be a comment",
350-
/* MULTIPLE
351-
LINE
352-
COMMENT
353-
\\o/ */
354-
"b" /* COMMENT */: /* YOU GUESSED IT */ 1 // COMMENT
355-
, /* STILL VALID */
356-
"c": 2
357-
}
358-
`,
359-
JsonParseMode.Loose,
360-
),
361-
).toEqual({
362-
a: 'this // should not be a comment',
363-
a2: 'this /* should also not be a comment',
364-
b: 1,
365-
c: 2,
366-
});
367-
});
368-
369-
it('works with json5.org example', () => {
370-
const input = `{
371-
// comments
372-
unquoted: 'and you can quote me on that',
373-
'singleQuotes': 'I can use "double quotes" here',
374-
lineBreaks: "Look, Mom! \\
375-
No \\\\n's!",
376-
hexadecimal: 0xdecaf,
377-
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
378-
positiveSign: +1,
379-
trailingComma: 'in objects', andIn: ['arrays',],
380-
"backwardsCompatible": "with JSON",
381-
}`;
382-
383-
expect(parseJson(input, JsonParseMode.Json5)).toEqual({
384-
unquoted: 'and you can quote me on that',
385-
singleQuotes: 'I can use "double quotes" here',
386-
lineBreaks: "Look, Mom! \nNo \\n's!",
387-
hexadecimal: 0xdecaf,
388-
leadingDecimalPoint: 0.8675309,
389-
andTrailing: 8675309,
390-
positiveSign: +1,
391-
trailingComma: 'in objects',
392-
andIn: ['arrays'],
393-
backwardsCompatible: 'with JSON',
394-
});
395-
});
396-
});
397325
});

0 commit comments

Comments
 (0)