JSON.stringfiy serialization

The JSON.stringify () method is a javascript value (object or array) is converted to a JSON string, if the specified replacer is a function, the value may be selectively replaced or if the specified replacer is an array, only the optional an array containing the specified property

 

grammar:

JSON.stringify(value [ , replacer [,space]])

 

Value: to be serialized into a JSON string value

Replacer ( optional ) : If the parameter is a function, then the serialization process, each attribute value is serialized are subjected to conversion and processing of the function; if the parameter is an array, only included in this attribute name array will be serialized into the final JSON string; if the parameter is null or not provided, the object properties will be serialized;

Space ( Optional ) : refers to the empty string with indentation for landscaping output ( Pretty-Print ) ; if the parameter is a number, which represents the number of spaces on the line 10 , which is less than 1 , which means no space; If the parameter is a string (the first ten letter string), the character string as a space; if no ( or a null) will not provide a space

 

description:

The JSON.stringify () will be installed for the respective values JSON format

  1. Daylight loading converted value has a toJSON () method, which defines what value will be serialized
  2. An array of non-object property appears in the string can not be guaranteed in the sequence of a particular order
  3. Boolean, numeric, string wrapper object serialization process will be converted to the corresponding original objects
  4. Undefined , arbitrary function symbol value, the serialization process can be ignored (in the non-array property values appear in the subject) or is converted into null ( appears in an array ) . Function, undefined when the transducer is mounted separately, will return undefined, as JSON.stringify (function () {}) or JSON.stringify (undefined)
  5. Circular reference object that contains (among objects refer to each other, forming a wireless loop) perform this method throws an error
  6. All with Symbol is a key attribute of the property will be completely ignored, even if the replacer parameter specifies the mandatory include them
  7. Date Date of call a toJSON () converted into string string (with Date.tolSoString () ) , and therefore can be treated as a string
  8. NaN and Infinity format and value null will be treated as null
  9. Other types of objects, including the Map / the Set / WeakMap / WeakSet , only the sequence of the enumerable properties

 

 1 // JSON.stringify()
 2 // let a=JSON.stringify({}); //{}
 3 // let a=JSON.stringify(true); //true
 4 // let a =JSON.stringify('foo'); //'foo'
 5 // let a= JSON.stringify([1,'foo',false]); //[1,'foo',false]
 6 // let a=JSON.stringify({x:5}); //{"x":5}
 7 
 8 // let a=JSON.stringify([new Number(1),new String('false'),new Boolean(false)]); //[1,"false",false]
 9 
10 
11 // let a=JSON.stringify(
12 //     Object.create(
13 //         null,
14 //         {
15 //             x:{value:'x',enumerable:false},
16 //             y:{value:'y',enumerable:true},
17 //
18 //         }
19 //     )
20 // ); //{"y":"y"}

 

 

 

2, replacer parameters

Replacer parameter may be a function or array. As a function, it has two parameters, a key ( Key ) value ( value ) will be serialized

(1)  If a return Number, converted to the corresponding character string adding JSON string

(2)  returns a String, which are added as the attribute value character string JSON in

(3)  returns a Boolean "true"  or "false"  is added to the attribute as the attribute JSON string

(4)  if the return character of other object recursive sequence into JSON string, call for each attribute replacer method, unless the object is a function, in this case does not JSON output string

(5)  if the return undefined, the property does not JSON output

 

NOTE: can not repacer method, removed from the array value ( values ) , if the return undefined or a function, will be null substituted

function

 1 function replacer(key,value){
 2     if(typeof value==='string'){
 3         return undefined;
 4     }
 5     return value;
 6 }
 7 
 8 var foo= {foundAtion:'Mozilla',model:'box',week:45};
 9 
10 var jsonString=JSON.stringify(foo,replacer);
11 console.log(jsonString); //{"week":45}

 

 

 

 

(array)

If replacer is an array, the array value sequence into JSON attribute name string

1 var foo= {foundAtion:'Mozilla',model:'box',week:45};
2 console.log(JSON.stringify(foo,['model','week'])); //{"model":"box","week":45}

 

3, space parameters

Space parameters for controlling pitch in the result string, if it is a number, each of the plurality of digital values are indented one space (up to the previous one in a string of 10 th); if it is a string, each level than on a multi-level indent string

 

1 console.log(JSON.stringify({a:2,b:4},null," "));
2 // {
3 //     "a": 2,
4 //     "b": 4
5 // }
6 
7  

 

 

toJSON method

If a sequence object has toJSON method, then toJSON method will override the default serialization behavior of the object, not the object serialization instead, and call toJSON method return values are serialized

 

1 var obj ={
2     foo:'foo',
3     toJSON(){
4         return 'bar'
5     }
6 };
7 console.log(JSON.stringify(obj)); //"bar"
8 console.log(JSON.stringify({x:obj})) //{"x":"bar"}

 

Guess you like

Origin www.cnblogs.com/hongding/p/11387796.html