Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does serialization of javascript mean?

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is the meaning of serialization of javascript". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what javascript serialization means.

Serialization is the process of converting an in-memory variable into a string that can be saved or transferred; in javascript, you can use the "JSON.stringify ()" method to serialize, which converts a JavaScript value to a JSON string.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Serialization is the process of converting a variable in memory into a string that can be saved or transferred. Deserialization is to convert the string into the original variable at the appropriate time. The combination of these two processes can easily complete the data storage and transmission operation, making the program more maintainable.

Javascript serialization

In javascript, you can use the "JSON.stringify ()" method to serialize, which converts the JavaScript value to a JSON string. Deserialization requires the "JSON.parse ()" method.

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

Parameter description:

Value:

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

Replacer:

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

If replacer is a function, JSON.stringify calls 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, members are excluded. The key of the root object is an empty string: ".

If replacer is an array, only members of the array that have key values are converted. The conversion order of members is the same as the order of keys in an array.

Space:

Optionally, the text adds indents, spaces, and newline characters, and if space is a number, the return value text indents a specified number of spaces at each level, and if space is greater than 10, the text is indented by 10 spaces. Space can also use non-numbers, such as:\ t.

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

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

① supports numbers, strings, Boolean values and null for the five primitive types in JavaScript. JSON syntax does not support undefined.

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

③ JSON syntax does not support functions

④ supports all objects except RegExp and Error objects. JSON syntax supports all other objects

The result of ⑤ date object serialization is a string in ISO format, but JSON.parse () still retains their string form and does not restore it to a date object

⑥ JSON.stringify () can only serialize enumerable own properties of an object

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

At this point, I believe that you have a deeper understanding of "what javascript serialization means". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report