JavaScript Object Serialization Detailed

Source: Internet
Author: User
Tags object serialization serialization tojson

I. What is Object serialization?

Object serialization refers to the conversion of an object's state to a string (from my novice's understanding, as some books say, 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", with strong academic, slightly high-end);

Two. Why are object serialization?

All things in the world have their reasons for existence. Why is there an object serialization? Because the program apes need it. Now that we're serializing objects, let's start with an object:

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

When this code runs, the contents of the object obj are stored in a block of memory, and obj itself stores only the mapping of the address of the memory. Simply put, the object obj is something that our program maintains in memory when the computer is powered on, and if our program stops or the computer loses power, the object obj will no longer exist. So how do you keep the contents of the object obj on disk (that is, keep it when there is no power)? At this point, you need to serialize the object obj, that is, to convert the contents of obj into a string, and then save it on disk. Also, how do we send the object obj content to the client via the HTTP protocol? Yes, you need to serialize the object obj first, and then the client resolves the object based on the string that was received and then deserialized (that is, the string is reverted to an object). This is the "Baidu Encyclopedia-serialization" described in the two role-storage, transmission.

Three. 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 very simple to use:

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

① for five primitive types in JavaScript, JSON syntax supports numbers, strings, booleans, null four, and does not support undefined;

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

③ JSON syntax does not support functions;

④ except RegExp, Error object, JSON syntax supports all other objects;

⑤ The result of a Date object serialization is an ISO-formatted string, but Json.parse () retains their string morphology and does not revert to a Date object;

⑥json.stringify () can only serialize an enumerable property of an object;

As can be seen from the above example, a deep copy of an object can also be accomplished through object serialization and deserialization (what is the deep copy of the object) in the case of the above rules? can go to see: https://www.zhihu.com/question/23031215, Shangyang answer).

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

Four. Full version json.stringify ()

1. Introduction

  Serializes the original value, object, or array

2. Overview

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

3. Parameters

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

Filter, optional parameter, an array or function

Indent, optional parameter, a numeric value or a string

4. Return

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

5. Description

  ① when the object o itself has the Tojson () method, Json.stringify () invokes the O's Tojson () method and uses the return value of the method instead of the object itself for string;

② if the filter exists and is a function, the return value of the function will be the return value of the Json.stringify () method. And the function receives two parameters, the first argument is an empty string, and the second argument is the object o. Here, you can also refer to the Douglas Crockford for ES3 Environment using JSON write json2:https://github.com/douglascrockford/json-js/blob/master/json2.js#l427. See the implementation of json.stringify and STR should understand the specific meaning of these two parameters that I am talking about here;

③ if the filter exists and is an array of strings (if the array contains numbers, the numbers are automatically converted to strings), some property names of object o are omitted from the array if they are not, and the order of the properties in the returned string is consistent with the order of the attributes in the array;

④json.stringify () usually returns a string without any space or line break. If you want to output a better-readable string, you need to specify a third parameter. If the third parameter specified is a value between 1~10, Json.stringify () inserts a newline character and a specified number of spaces at each "level" of output. If the third parameter specified 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 strings in JSON format

2. Overview

Json.parse (s[, Reviver])

3. Parameters

S, the string to parse

Reviver, optional parameter, optional function for converting resolved values

4. Return

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

5. Description

① if the Reviver function is specified, the function is called once for each original value parsed from S (not the object or array that contains the original values). Call Reviver with two parameters, the first parameter is the property name--The object's property name or the array ordinal converted to a string, the second parameter is the original value of the object property or array element (if there is doubt about the two arguments here, still can refer to: https://github.com/ douglascrockford/json-js/blob/master/json2.js#l460). Also, the return value of the Reviver function will be the return value of the Json.parse ();

JavaScript Object Serialization Detailed

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.