What is javascript serialization

青灯夜游
Release: 2023-01-11 09:18:58
Original
2595 people have browsed it

Serialization refers to the process of converting a variable in memory into a string that can be saved or transmitted; in JavaScript, you can use the "JSON.stringify()" method to achieve serialization, which can Convert JavaScript values ​​to JSON strings.

What is javascript serialization

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

The so-called serialization is the process of converting a variable in memory into a string that can be saved or transmitted. Deserialization is to convert this string into the original variable at the appropriate time for use. The combination of these two processes can easily complete data storage and transmission operations, making the program more maintainable.

javascript serialization

In javascript, you can use the "JSON.stringify()" method to achieve serialization, which can The value is converted to a JSON string. Deserialization requires the use of the "JSON.parse()" method.

Grammar format: JSON.stringify(value[, replacer[, space]])

Parameter description:

  • value:

    Required, the JavaScript value to be converted (usually an object or array).

  • replacer:

    Optional. The function or array used to convert the result.

    If replacer is a function, JSON.stringify will call the function, passing in the key and value of each member. Use the return value instead of the original value. If this function returns undefined, the member is excluded. The key of the root object is an empty string: "".

    If replacer is an array, only members with key values ​​in the array are converted. Members are converted in the same order as the keys in the array.

  • #space:

    Optional, the text adds indentation, spaces and newlines. If space is a number, the return value text is indented at each level specified number of spaces. If space is greater than 10, the text is indented by 10 spaces. Space can also use non-digits, such as: \t.

Example: Use JSON.stringify() to serialize an object.

What is javascript serialization

What is javascript serialization

As you can see, there is no y:undefined content in exampleStr and exampleObj2. This shows that: JSON syntax is a subset of JavaScript syntax. It cannot represent all values ​​in JavaScript. For attributes that are not supported by JSON syntax, they will be omitted after serialization. The detailed rules are as follows:

① For the five primitive types in JavaScript, JSON syntax supports four types: numbers, strings, Boolean values, and null, and undefined is not supported;

②NaN, Infinity, and -The result of Infinity serialization is null;

③JSON syntax does not support functions;

④Except RegExp and Error objects, JSON syntax supports all other objects;

⑤Date objects The results of serialization are strings in ISO format, but JSON.parse() still retains their string form and will not restore them to date objects;

⑥JSON.stringify() can only serialize objects Enumerable own properties;

As you can see from the above example, if the above rules are met, the deep copy of the object can also be completed through object serialization and deserialization.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What is javascript serialization. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!