Skip to content

Commit f1662f8

Browse files
committed
Fix microsoft#8729: Make JSON.stringify accept null and undefined replacers
1 parent 91451b3 commit f1662f8

File tree

5 files changed

+122
-20
lines changed

5 files changed

+122
-20
lines changed

src/lib/es5.d.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -949,38 +949,22 @@ interface JSON {
949949
* If a member contains nested objects, the nested objects are transformed before the parent object is.
950950
*/
951951
parse(text: string, reviver?: (key: any, value: any) => any): any;
952-
/**
953-
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
954-
* @param value A JavaScript value, usually an object or array, to be converted.
955-
*/
956-
stringify(value: any): string;
957-
/**
958-
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
959-
* @param value A JavaScript value, usually an object or array, to be converted.
960-
* @param replacer A function that transforms the results.
961-
*/
962-
stringify(value: any, replacer: (key: string, value: any) => any): string;
963-
/**
964-
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
965-
* @param value A JavaScript value, usually an object or array, to be converted.
966-
* @param replacer Array that transforms the results.
967-
*/
968-
stringify(value: any, replacer: any[]): string;
969952
/**
970953
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
971954
* @param value A JavaScript value, usually an object or array, to be converted.
972955
* @param replacer A function that transforms the results.
973956
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
974957
*/
975-
stringify(value: any, replacer: (key: string, value: any) => any, space: string | number): string;
958+
stringify(value: any, replacer?: (key: string, value: any) => any, space?: string | number): string;
976959
/**
977960
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
978961
* @param value A JavaScript value, usually an object or array, to be converted.
979-
* @param replacer Array that transforms the results.
962+
* @param replacer An array of strings and numbers that acts as a white list for selecting the object properties that will be stringified.
980963
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
981964
*/
982-
stringify(value: any, replacer: any[], space: string | number): string;
965+
stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string;
983966
}
967+
984968
/**
985969
* An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
986970
*/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [json.stringify.ts]
2+
3+
var value = null;
4+
JSON.stringify(value, undefined, 2);
5+
JSON.stringify(value, null, 2);
6+
JSON.stringify(value, ["a", 1], 2);
7+
JSON.stringify(value, (k) => undefined, 2);
8+
JSON.stringify(value, undefined, 2);
9+
10+
//// [json.stringify.js]
11+
var value = null;
12+
JSON.stringify(value, undefined, 2);
13+
JSON.stringify(value, null, 2);
14+
JSON.stringify(value, ["a", 1], 2);
15+
JSON.stringify(value, function (k) { return undefined; }, 2);
16+
JSON.stringify(value, undefined, 2);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
=== tests/cases/compiler/json.stringify.ts ===
2+
3+
var value = null;
4+
>value : Symbol(value, Decl(json.stringify.ts, 1, 3))
5+
6+
JSON.stringify(value, undefined, 2);
7+
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
8+
>JSON : Symbol(JSON, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
9+
>stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
10+
>value : Symbol(value, Decl(json.stringify.ts, 1, 3))
11+
>undefined : Symbol(undefined)
12+
13+
JSON.stringify(value, null, 2);
14+
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
15+
>JSON : Symbol(JSON, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
16+
>stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
17+
>value : Symbol(value, Decl(json.stringify.ts, 1, 3))
18+
19+
JSON.stringify(value, ["a", 1], 2);
20+
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
21+
>JSON : Symbol(JSON, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
22+
>stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
23+
>value : Symbol(value, Decl(json.stringify.ts, 1, 3))
24+
25+
JSON.stringify(value, (k) => undefined, 2);
26+
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
27+
>JSON : Symbol(JSON, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
28+
>stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
29+
>value : Symbol(value, Decl(json.stringify.ts, 1, 3))
30+
>k : Symbol(k, Decl(json.stringify.ts, 5, 23))
31+
>undefined : Symbol(undefined)
32+
33+
JSON.stringify(value, undefined, 2);
34+
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
35+
>JSON : Symbol(JSON, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
36+
>stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
37+
>value : Symbol(value, Decl(json.stringify.ts, 1, 3))
38+
>undefined : Symbol(undefined)
39+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
=== tests/cases/compiler/json.stringify.ts ===
2+
3+
var value = null;
4+
>value : null
5+
>null : null
6+
7+
JSON.stringify(value, undefined, 2);
8+
>JSON.stringify(value, undefined, 2) : string
9+
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (number | string)[] | null | undefined, space?: string | number | undefined): string; }
10+
>JSON : JSON
11+
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (number | string)[] | null | undefined, space?: string | number | undefined): string; }
12+
>value : null
13+
>undefined : undefined
14+
>2 : number
15+
16+
JSON.stringify(value, null, 2);
17+
>JSON.stringify(value, null, 2) : string
18+
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (number | string)[] | null | undefined, space?: string | number | undefined): string; }
19+
>JSON : JSON
20+
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (number | string)[] | null | undefined, space?: string | number | undefined): string; }
21+
>value : null
22+
>null : null
23+
>2 : number
24+
25+
JSON.stringify(value, ["a", 1], 2);
26+
>JSON.stringify(value, ["a", 1], 2) : string
27+
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (number | string)[] | null | undefined, space?: string | number | undefined): string; }
28+
>JSON : JSON
29+
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (number | string)[] | null | undefined, space?: string | number | undefined): string; }
30+
>value : null
31+
>["a", 1] : (string | number)[]
32+
>"a" : string
33+
>1 : number
34+
>2 : number
35+
36+
JSON.stringify(value, (k) => undefined, 2);
37+
>JSON.stringify(value, (k) => undefined, 2) : string
38+
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (number | string)[] | null | undefined, space?: string | number | undefined): string; }
39+
>JSON : JSON
40+
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (number | string)[] | null | undefined, space?: string | number | undefined): string; }
41+
>value : null
42+
>(k) => undefined : (k: string) => undefined
43+
>k : string
44+
>undefined : undefined
45+
>2 : number
46+
47+
JSON.stringify(value, undefined, 2);
48+
>JSON.stringify(value, undefined, 2) : string
49+
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (number | string)[] | null | undefined, space?: string | number | undefined): string; }
50+
>JSON : JSON
51+
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (number | string)[] | null | undefined, space?: string | number | undefined): string; }
52+
>value : null
53+
>undefined : undefined
54+
>2 : number
55+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @strictNullChecks: true
2+
3+
var value = null;
4+
JSON.stringify(value, undefined, 2);
5+
JSON.stringify(value, null, 2);
6+
JSON.stringify(value, ["a", 1], 2);
7+
JSON.stringify(value, (k) => undefined, 2);
8+
JSON.stringify(value, undefined, 2);

0 commit comments

Comments
 (0)