Detailed explanation of JavaScript object serialization _javascript techniques

Source: Internet
Author: User
Tags numeric value object serialization serialization tojson

A. What is Object serialization?

Object serialization refers to the conversion of the state of an object to a string (from my rookie's understanding, as some books say), plain and easy to understand! );

Serialization (serialization) is the process of transforming the state information of an object into a form that can be stored or transmitted (from "Baidu Encyclopedia-serialization", academic strong, slightly high-end);

Two. Why is there a serialization of objects?

All things in the world have their reasons for existence. Why is there a serialization of objects? Because the program apes need it. Since it's object serialization, let's start with an object:

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

When this code is run, the contents of the object obj are stored in a piece of memory, and obj itself stores only the mapping of the address of the memory. Simply put, object obj is something our program maintains in memory when the computer is powered up, and object obj will no longer exist if our program stops or the computer loses power. So how do you keep the contents of object obj on disk (that is, keep it when there is no power)? Object obj needs to be serialized, which means converting the contents of obj into a string and then saving it on disk. In addition, how do we send object obj content to the client through the HTTP protocol? Yes, the object obj needs to be serialized first, and then the client resolves the object based on the string that is received and then deserialized (that is, the string is restored to the object). This is also the "Baidu Encyclopedia-serialization" in the description of the two functions-storage, transmission.

Three. The serialization of objects in JavaScript

The full name of JSON is the "JavaScript object Notation"--javascript object notation. The built-in functions json.stringify () and Json.parse () are provided in ECMAScript 5 to serialize and restore JavaScript objects. They are also simple to use:

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

    • ① for five primitive types in JavaScript, the JSON syntax supports numbers, strings, Boolean values, and null four types, and does not support undefined;
    • The results of ②nan, infinity, and-infinity serialization are null;
    • ③json syntax does not support functions;
    • ④ except for RegExp and Error objects, the JSON syntax supports all other objects;
    • ⑤ Date Object serialization results in an ISO-formatted string, but Json.parse () retains their string shape and does not revert to a Date object;
    • ⑥json.stringify () can only serialize the object's enumerable own properties;

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

These are the common uses of json.stringify () and Json.parse (), but these two methods are more than that:

Four. Full version json.stringify ()

1. Introduction

Serializing the original value, object, or array

2. Summary

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

3. Parameter

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

Filter, optional parameter, an array or function

Indent, optional parameter, a numeric value or a string

4. Returns

A JSON-formatted string that represents the value of O, filtered by filter, and formatted according to indent

5. Describe

① when object o itself has the Tojson () method, Json.stringify () invokes the Tojson () method of O and uses the return value of the method rather than the object itself to string;

② if filter exists and is a function, then the return value of the function will be the return value of the Json.stringify () method. And the function receives two arguments, the first argument is an empty string, and the second argument is Object O.

③ if filter exists and is an array of strings (if the array contains numbers, numbers are automatically converted to strings), some property names of object o, if not in this array, are omitted when serialized, and the order of the attributes in the returned string is the same as the order of the attributes in the array;

④json.stringify () usually returns a string without any spaces or line breaks. If you want to output a more readable string, you need to specify a third parameter. If the specified third argument is a value between 1~10, Json.stringify () inserts a newline character and a specified number of spaces at each "level" output. If the specified third argument is a non-empty string, json.stringify () inserts a newline character and the string (only the first 10 characters) to indent the hierarchy;

Five. Full version json.parse ()

1. Introduction

Parsing JSON-formatted strings

2. Summary

Json.parse (s[, Reviver])

3. Parameter

S, the string to parse

Reviver, optional parameter, an optional function for converting analytic values

4. Returns

An object, array, or original value. The return value is parsed from S (and possibly modified by reviver);

5. Describe

① if the Reviver function is specified, the function is invoked once for each original value that is resolved from S (not an object or array that contains the original values). Call Reviver with two arguments, the first parameter is the property name--The object's property name or the array ordinal converted to a string, and the second parameter is the object property or the original value of the array element. Also, the return value of the Reviver function will be the return value of the Json.parse ();

The above is about JavaScript object serialization related content, hope to be helpful to everybody's study.

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.