IE8 native JSON support, leading Firefox

Source: Internet
Author: User
Tags object serialization tojson javascript eval
Native JSON support in IE8

Translation: Meng xianhui

Original article: http://blogs.msdn.com/ie/archive/2008/09/10/native-json-in-ie8.aspx

You may have guessed from the title of this article that Internet Explorer 8 (currently beta2) provides native JSON parsing and serialization. This new Native JSON function enables Internet Explorer 8 to run on existing Ajax applications more quickly and securely.

What is JSON?

Most developers do not only develop Ajax programs. Here I will introduce some background knowledge. JSON is a simple data exchange format that can be read by people. In Ajax programs, this format is usually used when data is transmitted between servers and web programs.

For example, if you select a contact name from the favorite web mail, you can see the contact information. The data stream sent by the server to a web program (running in a browser) may look like the following:

{ 
"firstName": "cyra",
"lastName": "richardson",
"address": {
"streetAddress": "1 Microsoft way",
"city": "Redmond",
"state": "WA",
"postalCode": 98052
},

"phoneNumbers": [
"425-777-7777",
"206-777-7777"
]
}

Value
Fortunately, this format is fully compatible with the Javascript syntax. Many today's programs use the Javascript eval () function to convert the obtained data
JavaScript Object. Using eval () is insecure and resource-consuming. Eval () parses the string into a JScript expression and runs it. If
If the eval () string has been tampered with, it may contain unexpected data, or even other people's code, which can be injected into your web program.

Currently, many
A library written in JavaScript to parse untrusted JSON data more securely. Some Resolvers written using JScript
(Http://www.json.org/json_parser.js) the data is strictly verified, some libraries, such as json2, JS (http:
// Www.json.org/json2.js), use a regular expression to perform a comprehensive check on the input string, and then use eval () for fast parsing. The ideal solution is
The native implementation method prevents the application from being injected with code and runs fast and can be used everywhere.

Original JSON in IE8 JScript

IE8
The jscript engine has completely native Implementation of JSON.
Draft, address http://wiki.ecmascript.org
/Doku. php? Id = es3.1: es3.1 _ proposal_working_draft)
Speed of serialization and deserialization, and improve the security of parsing untrusted data.

API

We have defined a new
Built-in object "JSON", which can be modified or overwritten. It looks like math or other built-in Global Objects. In addition to JSON objects, tojson () Functions
Added to the prototype of date, number, string, and Boolean objects. The JSON object has two methods: parse () and stringify ().

For example:

var jsObjString = "{/"memberNull/" : null, /"memberNum/" : 3, /"memberStr/" : /"StringJSON/", /"memberBool/" : true , /"memberObj/" : { /"mnum/" : 1, /"mbool/" : false}, /"memberX/" : {}, /"memberArray/" : [33, /"StringTst/",null,{}]"; 
var jsObjStringParsed = JSON.parse(jsObjString);
var jsObjStringBack = JSON.stringify(jsObjStringParsed);

The objects generated by the parse () method and serialized by the stringify () method are exactly the same as the following objects:

var jsObjStringParsed = 
{
"memberNull" : null,
"memberNum" : 3,
"memberStr" : "StringJSON",
"memberBool" : true ,
"memberObj" :
{
"mnum" : 1,
"mbool" : false
},
"memberX" : {},
"memberArray" :
[
33,
"StringTst",
null,
{}
]
};

JSON. parse (source, reviver)

The JSON. parse method executes deserialization. It uses a JSON string (specified by the source parameter) to generate a JScript object or array.

Yes
The selected parameter revive is a user-defined function used to calculate resolution changes. The result object or array is recursively traversed. The reviver function is used on each member, and each member value is
The return value of reviver is replaced. If reviver returns NULL, the object member is deleted. The traversal and calling of reviver are completed in the descending order. That is, the object's
After all the Members are "Revived", the entire object will be "Revived.

Reviver is mainly used to identify similar
ISO to convert them into date objects. So far, JSON format (http://www.json.org/) pairs
The date object cannot be converted back and forth, because there is no standard date text in JScript. Es3.1 draft (

Http://wiki.ecmascript.org

/Doku. php? Id = es3.1: es3.1 _ proposal_working_draft) contains how to use
Example of how the reviver function solves this problem.

JSON. stringify (value, replacer, space)

This
Is the serialization method. It uses the object or array specified by the value parameter as the parameter to generate a string in JSON format. Recursively accessing objects or arrays and serializing them to specific
JSON format. If the value parameter has the tojson () method, this method acts as the first filter, and the original value is
Value. tojson (key) substitution, the final value is serialized. The parameter key is a string. When an object like (key: Value) is serialized,
Key is the name of a member. For the root object, the key is a null string.

Date. Prototype. tojson ()
Generate a non-escape string, which is a real serializer, because stringify () returns the original string without any changes. The date object is serialized using the tojson () method.

Number. Prototype. tojson (), String. Prototype. tojson (), Boolean. Prototype. tojson ()
The function returns valueof (). They are used for correct Object serialization, such as "Var num = new number (3.14.

Yes
The selected replacer parameter acts as a filter and is used recursively. It can be a function or an array. If replacer is a function
Key: value all call replacer (Key, value ). For the root object, call replacer ("", value ). If
Replacer is an array, which must be an array string. The element of the array is the name of the member to be serialized. The serialization order follows the name order in the array. When serializing an array
Replacer is ignored.

The optional parameter space is about how to format the output text. If this parameter is omitted, the output text does not contain any extra spaces. If it is a number, it specifies the number of spaces for each level of indentation. If it is a character (such as "/t" or "& nbsp;"), it indent each level of characters with these characters.

What is the impact on existing web pages?

Es3.1 JSON proposal is the main factor used by popular json2.js. We also use the JSON name. Global Object
JSON can be rewritten. However, it is no longer an undefined object. This is the same as introducing the new keyword in the script language. Using a name occasionally affects the existing code. Use
Json2.js pages are unlikely to be affected. Except for a few exceptions, all these pages will continue to work normally, but they can only run faster.

The pages defined by the self-implemented JSON object may be affected, especially when the "If (! This. JSON) {JSON = ...}" JSON object defined in this mode. There are two main ways to solve this problem:

1. migrate existing code using a native JSON object

If your own JSON implementation is based on a certain version of json2.js, it will be easy to migrate.

2. decide not to use native JSON support and continue to use your existing JSON object.

This
You can rename or override the JSON name. Rename means you need to change all the code using the JSON name to something similar to"
Myjson. Rewriting means that your own JSON definition will overwrite all the code defined using the default native JSON. In most cases, you only need to remove the condition"
If (! This. JSON.

Considering the impact of the 3.1 Standard, the JSON name is consistent with our desire to implement interoperability through the defined interfaces.

Off
For native JSON, there are many things to talk about. The parser is not based on eval ()
Is an independent implementation. It is supported with JSON (http://wiki.ecmascript.org
/Doku. php? Id = es3.1: json_support. It is also with http://www.json.org/json_parser.js
It is safe and fast. Therefore, if you use eval () or your own JSON library, check the original JSON implementation in IE8 for better performance and security operations.

Corneliu barsan

Senior Development Engineer

JScript team

 

Original web site http://msdn.microsoft.com/zh-cn/dd430114.aspx

Related URLs

JSON. parse Method
The http://msdn.microsoft.com/en-us/library/cc836466 (vs.85). aspx

JSON. stringify Method
The http://msdn.microsoft.com/en-us/library/cc836459 (vs.85). aspx

Tojson Method
The http://msdn.microsoft.com/en-us/library/cc907896 (vs.85). aspx

 

 

 

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.