Introduction to object serialization in JavaScript and javascript serialization
Like Java, JavaScript can serialize and deserialize objects to save objects. In ECMAScript 5, Object serialization in JavaScript is implemented through JSON. stringify (), and deserialization is implemented through JSON. parse:
Copy codeThe Code is as follows:
Var o = {x: 1, y: 29, z: 42 };
Var s = JSON. stringify (o );
Console. log (s); // {"x": 1, "y": 29, "z": 42}
Var c = JSON. parse (s );
Console. log (c); // Object {x = 1, y = 29, z = 42}
For browsers that only support the ECMAScript 3 standard, you can use Douglas Crockford to write json2.js (https://github.com/douglascrockford/JSON-js ).
During Object serialization, NaN, Infinity, and-Infinity are serialized to "null"; Date objects are serialized to strings representing the corresponding time (however, JSON is used. during parse () deserialization, the time string will exist as a normal string and will not be rebuilt as a Date object ).
When JSON. stringify () is used to serialize an object, the serialized property is limited to the property of the enumerable of the object itself (Own. In JSON. when stringify () is running, JavaScript first checks whether the toJSON () method exists in the object to be serialized. If the toJSON () method exists, the method is called and the returned result is used as the serialization target. If the toJSON () method does not exist, use the default serialization method.