Detailed description of JavaScript Object serialization _ javascript skills

Source: Internet
Author: User
Tags object serialization tojson
This article describes the serialization of JavaScript objects. For more information, see 1. What is object serialization?

Object serialization refers to converting the object state to a string (from my understanding of cainiao, it seems that some books also say so, easy to understand !);

Serialization refers to the process of converting object state information into a form that can be stored or transmitted (from "Baidu encyclopedia-Serialization", highly academic, slightly high-end );

2. Why object serialization?

Everything in the world has its own reasons. Why object serialization? Because programmers need it. Since it is object serialization, let's start with an object:

var obj = {x:1, y:2};

When this code is run, the object obj content will be stored in a piece of memory, while obj itself only stores the ing of the memory address. To put it simply, object obj is a kind of thing that our program maintains in memory when the computer is powered on. If our program is stopped or the computer is powered off, object obj will no longer exist. So how to save the object obj content on the disk (that is to say, keep it when there is no power )? In this case, you need to serialize the object obj, that is to say, convert the content of obj into a string, and then save it on the disk. In addition, how can we send the object obj content to the client through HTTP? Yes, you still need to serialize the object obj first, and then parse the corresponding object by the client based on the received string (that is, restoring the string to an object. This is exactly the two functions described in "Baidu encyclopedia-serialization"-storage and transmission.

Iii. Object serialization in JavaScript

The full name of JSON is "JavaScript Object Notation" -- JavaScript Object Notation. ECMAScript 5 provides built-in functions JSON. stringify () and JSON. parse () for serialization and restoration of JavaScript objects. They are also easy to use:

As you can see, exampleStr and exampleObj2 do not contain y: undefined. This note: JSON syntax is a subset of JavaScript syntax. It cannot represent all values in JavaScript. Attributes not supported by JSON syntax are omitted after serialization. The detailed rules are as follows:

  • ① For the five primitive types in JavaScript, The JSON syntax supports numbers, strings, Boolean values, and null, and does not support undefined;
  • ② The result of NaN, Infinity, and-Infinity serialization is null;
  • ③ JSON syntax does not support functions;
  • ④ Besides RegExp and Error objects, JSON syntax supports all other objects;
  • ⑤ Date objects are serialized in ISO format, but JSON. parse () still retains their string form and will not be restored to date objects;
  • ⑥ JSON. stringify () can only serialize the enumerated attributes of objects;

As shown in the preceding example, if the above rules are met, deep copy of objects can be completed through object serialization and deserialization.

The above are common usage of JSON. stringify () and JSON. parse (), but these two methods are not so simple:

4. Full JSON. stringify ()

1. Introduction

Serialize the original value, object, or Array

2. Summary

JSON. stringify (o [, filter] [, indent])

3. Parameters

O, the original value, object, or array to be converted to a JSON string

Filter, an optional parameter, an array or function

Indent, an optional parameter, a value or a string

4. Return

A string in JSON format, representing the o value, filtering by filter, and formatting by indent

5. Description

① When the object o itself carries the toJSON () method, JSON. stringify () calls o's toJSON () method and uses the return value of this method instead of the object itself for stringization;

② If the filter exists and is a function, the return value of this function will be used as the return value of the JSON. stringify () method. The function receives two parameters. The first parameter is an empty string, and the second parameter is the object o.

③ If the filter exists and is a string array (if the array contains numbers, the number is automatically converted to a string), some attribute names of object o are not in this array, these attributes will be omitted during serialization, and the order of the attributes in the returned string will be consistent with the order of the attributes in the array;

④ JSON. stringify () returns a string without any space or line breaks. If you want to output a string with better readability, you must specify the third parameter. If the third parameter is between 1 and 1 ~ JSON. stringify () inserts a line break and a specified number of spaces in each "level" output. If the third parameter is a non-null string, JSON. stringify () inserts a line break and the string (only the first 10 characters) to indent the level;

5. Full JSON. parse ()

1. Introduction

Parses JSON strings.

2. Summary

JSON. parse (s [, reviver])

3. Parameters

S, the string to be parsed

Reviver, an optional parameter. It is an optional function used to convert the parsed value.

4. Return

An object, array, or original value. The returned value is parsed from s (it may have been modified by reviver );

5. Description

① If the reviver function is specified, this function will call every original value parsed from s (not an object or array containing these original values. When you call reviver, there are two parameters. The first parameter is the attribute name of the object or the serial number converted to a string. The second parameter is the original value of the object attribute or array element. In addition, the return value of the reviver function is used as the return value of JSON. parse;

The above is related to JavaScript Object serialization, and I hope it will be helpful for your learning.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.