Built-in JSON parsing module of dojo

Source: Internet
Author: User
Tags tojson
Link: http://www.sitepen.com/blog/2012/01/05/native-json-parsing-in-dojo/
Original Author: Kris zyp

Dojo1.7 introduces a new module: dojo/JSON. The difference between this module and the original dojo. fromjson and dojo. tojson functions is that it is based on the json api supported by the built-in JavaScript language. It also uses built-in JSON parsing and serialization functions (if any), so it can achieve the fastest speed in any browser.

The new API is easy to use. If you have used the json.org function library or built-in function in the browser, it will be easier to use. Once the "dojo/JSON" module is loaded and the variable JSON is assigned, we can use the parse and stringify functions. For example, to parse a JSON string:

 
Define (["dojo/JSON"], function (JSON) {var jsonstr = '{"name": "value"}'; var object = JSON. parse (jsonstr); object. name-> "value ";});

If the browser supports this function, the built-in parsing function will be used in this parse function, otherwise it will degrade to the dojo implementation.

In turn, you need to serialize an object into a JSON string:

 
Define (["dojo/JSON"], function (JSON) {var object = {"name": "value"}; var jsonstr = JSON. stringify (object); jsonstr-> '{"name": "value "}'});

Serialization of date data

The new JSON module can now serialize date data correctly. Previously, dojo. fromjson serialized the date object to "{}", and now it is automatically converted to the standard Date Format (ISO standard format in UTC ).

Performance of built-in functions-lightweight

The new JSON module uses the has () function to determine whether to use the built-in JSON function. This is not just a function detection, it also means that when you need to package for a specific browser, for the latest mainstream browsers, this module only has a few bytes, this is very valuable for mobile applications.

Legacy function dojo. fromjson and dojo. tojson will continue to exist in the dojo base for a period of time. However, we recommend using the parse and stringify functions in this dojo/JSON package for performance and consistency with the standard.

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.