This new native JSON feature enables Internet Explorer 8 to run faster and more securely on existing AJAX applications.
What is JSON?
Most developers do not only develop AJAX programs. Let me introduce some background knowledge here. JSON is a simple, human-readable data exchange format. In AJAX programs, this format is usually used when transmitting data between the server and the web program.
For example, if you select a contact name from your favorite webmail, you can see the contact information. The data stream sent by the server to the web program (running in the 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"
]
}
Thankfully, this format is fully compatible with JavaScript’s syntax. Many programs today use Javascript's eval() function to convert this obtained data into a Javascript object. Using eval() is unsafe and consumes resources. eval() parses this string into a Jscript expression and executes it. If the string passed to eval() has been tampered with, it may contain data we don't expect, or even someone else's code, which can be injected into your web application.
Nowadays, there are many libraries written in Javascript to parse untrusted JSON data more securely. Some parsers written in Jscript (http://www.json.org/json_parser.js) perform strict verification on data, and some libraries, like json2, js (http://www.json.org/json2. js), uses regular expressions to comprehensively check the input string, and then uses eval() to quickly parse it. The ideal solution would be a native implementation that protects the application from code injection, runs quickly, and can be used everywhere.
Native JSON in IE8 Jscript
The Jscript engine of IE8 already has a complete native implementation of JSON, and is in line with the ES3.1 Proposal Working Draft, address While maintaining the compatibility of JSON support described in http://wiki.ecmascript.org/doku.php?id=es3.1: es3.1_proposal_working_draft), it greatly improves the speed of serialization and deserialization, and Improve the security of parsing untrusted data.
API
We define a new built-in object "JSON", which can be modified or overridden. Looks a lot like math or other built-in global objects. In addition to JSON objects, specific functions like toJSON() have also been added to the prototypes of Date, Number, String, and boolean objects. JSON objects have 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 object generated by the parse() method and serialized back through the stringify() method is exactly the same as the following object:
var jsObjStringParsed =
{
"memberNull" : null,
"memberNum" : 3,
"memberStr" : "StringJSON",
"memberBool" : true ,
"memberObj" :
{
"mnum" : 1,
"mbool" : false
},
"memberX" : {},
"memberArray" :
]
};
JSON.parse(source, reviver )
The JSON.parse method performs deserialization. It uses a JSON-formatted string (specified by the parameter source) to generate a Jscript object or array.
The optional parameter revive is a user-defined function used to account for changes in parsing. The result object or array is traversed recursively, the reviver function is used on each member, and each member value is replaced by the return value of the reviver. If reviver returns null, the object members are deleted. The traversal and call to the reviver are completed in post-order traversal. That is to say: after all members of the object are "revived", the entire object is also "revived".
reviver is mainly used to identify strings like ISO and convert them into Date objects. So far, JSON format (http://www.json.org/) cannot be converted back and forth for Date objects. This is because there is no standard Date text size in Jscript. The ES3.1 draft (http://wiki.ecmascript.org/doku.php?id=es3.1: es3.1_proposal_working_draft) contains an example of how to use the reviver function to solve this problem.
JSON.stringify(value, replacer, space)
This is the serialization method. It takes the object or array specified by the value parameter as a parameter and generates a string in JSON format. Objects or arrays are accessed recursively and serialized into a specific JSON format. If the value parameter has a toJSON() method, then this method acts as the first filter, the original value is replaced by value.toJSON(key), and the final value is serialized. The parameter key is a string. When an object like (key:value) is serialized, the key is the name of the member. For the root object, the key is the empty string.
Date.prototype.toJSON() generates a string that does not require escaping and is a true serializer, because stringify() returns the original string without any changes. Date objects are serialized through the toJSON() method.
Number.prototype.toJSON (), String.prototype.toJSON(), Boolean.prototype.toJSON() function returns ValueOf(). They are used for proper serialization of objects, like "var num = new Number(3.14);"
The optional replacer parameter acts as a filter and is used recursively. It can be a function or an array. If replacer is a function, replacer(key,value) is called for each object member key:value. As for the root object, call replacer("",value). If replacer is an array, it must be an array string. The elements of the array are the names of the members to be serialized. The order of serialization is based on the order of names in the array. When serializing an array, array replacers are ignored.
The optional parameter space is about how to format the output text. If this parameter is omitted, the output text will not have 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 " "), it indents each level of characters by those characters.
What impact will it have on existing web pages?
The ES3.1 JSON proposal is the main factor used by the popular json2.js. We also use the name JSON. The global object JSON can be overridden. However, it is no longer an undefined object. This is the same as by introducing the new keyword in a scripting language. Adopting a name occasionally affects existing code. Pages using json2.js are unlikely to be affected.With very few exceptions, all of these pages will continue to work normally, just faster.
Pages that implement their own JSON object definitions may be affected, especially JSON objects defined using patterns like "if(!this.JSON) { JSON=...}". There are two main ways to solve this problem:
1. Migrate the existing code and use native JSON objects
If your JSON implementation is based on some version of json2.js, migrate it It's very simple.
2. Decide not to use native JSON support and continue to use your existing JSON object
This can be achieved by renaming or rewriting the JSON name. Renaming means changing all code that uses JSON names to something like "MyJSON". Rewriting means ensuring that your own JSON definition overrides all code that uses the default native JSON definition. In most cases, simply removing the condition "if(!this.JSON)" will work.
Given the influence of the 3.1 standard, using the name JSON is consistent with our desire to interoperate through well-defined interfaces.
There is a lot to talk about about native JSON. The parser is not based on eval(), it is a separate implementation. It is equivalent to the reference parser provided by JSON support (http://wiki.ecmascript.org/doku.php?id=es3.1: json_support). It is also as secure as http://www.json.org/json_parser.js and runs much faster. So, if you use eval(), or your own JSON library, please check out the native JSON implementation in IE8 for better performance and safer operations.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor