Skip to content

Commit

Permalink
Merge pull request microsoft#8732 from Microsoft/Fix8729
Browse files Browse the repository at this point in the history
Fix microsoft#8729: Make JSON.stringify accept `null` and `undefined` replacers
  • Loading branch information
mhegazy committed May 21, 2016
2 parents 91451b3 + f1662f8 commit 3f9efa0
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 20 deletions.
24 changes: 4 additions & 20 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -949,38 +949,22 @@ interface JSON {
* If a member contains nested objects, the nested objects are transformed before the parent object is.
*/
parse(text: string, reviver?: (key: any, value: any) => any): any;
/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
* @param value A JavaScript value, usually an object or array, to be converted.
*/
stringify(value: any): string;
/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
* @param value A JavaScript value, usually an object or array, to be converted.
* @param replacer A function that transforms the results.
*/
stringify(value: any, replacer: (key: string, value: any) => any): string;
/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
* @param value A JavaScript value, usually an object or array, to be converted.
* @param replacer Array that transforms the results.
*/
stringify(value: any, replacer: any[]): string;
/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
* @param value A JavaScript value, usually an object or array, to be converted.
* @param replacer A function that transforms the results.
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*/
stringify(value: any, replacer: (key: string, value: any) => any, space: string | number): string;
stringify(value: any, replacer?: (key: string, value: any) => any, space?: string | number): string;
/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
* @param value A JavaScript value, usually an object or array, to be converted.
* @param replacer Array that transforms the results.
* @param replacer An array of strings and numbers that acts as a white list for selecting the object properties that will be stringified.
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*/
stringify(value: any, replacer: any[], space: string | number): string;
stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string;
}

/**
* An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/baselines/reference/json.stringify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [json.stringify.ts]

var value = null;
JSON.stringify(value, undefined, 2);
JSON.stringify(value, null, 2);
JSON.stringify(value, ["a", 1], 2);
JSON.stringify(value, (k) => undefined, 2);
JSON.stringify(value, undefined, 2);

//// [json.stringify.js]
var value = null;
JSON.stringify(value, undefined, 2);
JSON.stringify(value, null, 2);
JSON.stringify(value, ["a", 1], 2);
JSON.stringify(value, function (k) { return undefined; }, 2);
JSON.stringify(value, undefined, 2);
39 changes: 39 additions & 0 deletions tests/baselines/reference/json.stringify.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
=== tests/cases/compiler/json.stringify.ts ===

var value = null;
>value : Symbol(value, Decl(json.stringify.ts, 1, 3))

JSON.stringify(value, undefined, 2);
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>JSON : Symbol(JSON, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>value : Symbol(value, Decl(json.stringify.ts, 1, 3))
>undefined : Symbol(undefined)

JSON.stringify(value, null, 2);
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>JSON : Symbol(JSON, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>value : Symbol(value, Decl(json.stringify.ts, 1, 3))

JSON.stringify(value, ["a", 1], 2);
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>JSON : Symbol(JSON, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>value : Symbol(value, Decl(json.stringify.ts, 1, 3))

JSON.stringify(value, (k) => undefined, 2);
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>JSON : Symbol(JSON, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>value : Symbol(value, Decl(json.stringify.ts, 1, 3))
>k : Symbol(k, Decl(json.stringify.ts, 5, 23))
>undefined : Symbol(undefined)

JSON.stringify(value, undefined, 2);
>JSON.stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>JSON : Symbol(JSON, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>value : Symbol(value, Decl(json.stringify.ts, 1, 3))
>undefined : Symbol(undefined)

55 changes: 55 additions & 0 deletions tests/baselines/reference/json.stringify.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
=== tests/cases/compiler/json.stringify.ts ===

var value = null;
>value : null
>null : null

JSON.stringify(value, undefined, 2);
>JSON.stringify(value, undefined, 2) : string
>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; }
>JSON : 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; }
>value : null
>undefined : undefined
>2 : number

JSON.stringify(value, null, 2);
>JSON.stringify(value, null, 2) : string
>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; }
>JSON : 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; }
>value : null
>null : null
>2 : number

JSON.stringify(value, ["a", 1], 2);
>JSON.stringify(value, ["a", 1], 2) : string
>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; }
>JSON : 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; }
>value : null
>["a", 1] : (string | number)[]
>"a" : string
>1 : number
>2 : number

JSON.stringify(value, (k) => undefined, 2);
>JSON.stringify(value, (k) => undefined, 2) : string
>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; }
>JSON : 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; }
>value : null
>(k) => undefined : (k: string) => undefined
>k : string
>undefined : undefined
>2 : number

JSON.stringify(value, undefined, 2);
>JSON.stringify(value, undefined, 2) : string
>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; }
>JSON : 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; }
>value : null
>undefined : undefined
>2 : number

8 changes: 8 additions & 0 deletions tests/cases/compiler/json.stringify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @strictNullChecks: true

var value = null;
JSON.stringify(value, undefined, 2);
JSON.stringify(value, null, 2);
JSON.stringify(value, ["a", 1], 2);
JSON.stringify(value, (k) => undefined, 2);
JSON.stringify(value, undefined, 2);

0 comments on commit 3f9efa0

Please sign in to comment.