When we serialize a value to a JSON string using json.stringify (), only JSON-safe values can be converted to strings via json.stringify (). So, what are JSON-safe values? Any value that can be effectively represented as JSON.

Non-json-safe values, such as undefined, function, and symbol, and looping references to object (attribute references to object structures create an infinite loop by creating each other). These values are illegal for a standard JSON structure, mainly because they cannot be migrated to other JSON-supported languages.

When json.stringify () encounters undefined, function, and symbol, their values are automatically ignored, and if such values are replaced with null in the array (so that the array index information is not changed). If such a value appears in an object’s property, that property is excluded.

JSON.stringify( undefined ); // undefined JSON.stringify( function(){} ); // undefined If an object with an invalid JSON value is serialized as a JSON string, you need to define a toJSON() method that returns a JSON-safe version of the object. JSON.stringify( [1,undefined,function(){},4] ); // "[1,null,null,4]" JSON.stringify( { a:2, b:function(){} } ); // "{"a":2}"Copy the code

Json.stringify () formats a circular reference to an object that throws an exception

JSON strings have a special behavior. If an object defines a toJSON() method, that method is called first and the value it returns is serialized.

If you serialize an object with an invalid JSON value into a JSON string, you need to define a toJSON() method that returns a JSON-safe version of the object.

var o = { };

var a = {
    b: 42,
    c: o,
    d: function(){}
};

// create a circular reference inside `a`
o.e = a;

// would throw an error on the circular reference
// JSON.stringify( a );

// define a custom JSON value serialization
a.toJSON = function() {
    // only include the `b` property for serialization
    return { b: this.b };
};

JSON.stringify( a ); // "{"b":42}"
Copy the code

A common misconception is that toJSON() should return the JSON string of an object. This is incorrect; toJSON should return a normal value of any type, and json.stringify () itself serializes this value.

Json.stringify () the second argument is optional and is called replacer. This argument can be an array or a function. It provides a filtering mechanism to specify which attributes should or should not be included, enabling customizable recursive serialization of objects, much like how toJSON() serializes a value.

If the replacer were an array, it would be an array of strings. Each value specifies the object’s property name, indicating that the property should be added to the serialization. If a property is not in the list, it will be skipped.

If replacer were a function, it would first be called once by the object, and then once for each property of the object, passing the function two values, key and value, each time. To skip a key in the serialization process, simply return undefined, otherwise return the supplied value.

const obj = { a: 0, b: 1, c: [0, 1, 2]} JSON. The stringify (obj, (' a ', 'c')) / / "{" a" : 0, "c" : [0]} "JSON. The stringify (obj, (key, value) = > {the if (key! = = 'b') {return value}}) / / "{" a" : 0, "c" : [1, 2, 3]}"Copy the code

When replacer is a function, the obj object itself is passed in the first call. The if statement filters out attributes named “b”. The string is recursive, so each value in the array [1,2,3] is passed to the replacer’s parameter value with the corresponding subscript (0,1,2) as the parameter key.

A third optional argument that can also be passed to json.stringify (), called space, is used as an indentation to make the output nicely formatted. Space can be a positive integer that specifies how many Spaces should be indented per level. Or space can be a string, and the first ten characters of its value are used for each indentation level.

Const a = {b: 42, c: "42", d: [1,2,3]}; JSON.stringify( a, null, 3 ); // "{ // "b": 42, // "c": "42", // "d": [ // 1, // 2, // 3 // ] // }" JSON.stringify( a, null, "-----" ); // "{ // -----"b": 42, // -----"c": "42", // -----"d": [ // ----------1, // ----------2, // ----------3 // -----] // }"Copy the code

Author: EickL links: www.jianshu.com/p/94d9e1bca… The copyright of the book belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.