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.