source: Dev/branches/rest-dojo-ui/client/dojo/_base/json.js @ 263

Last change on this file since 263 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 3.1 KB
Line 
1define(["./kernel", "../json"], function(dojo, json){
2  // module:
3  //    dojo/_base/json
4  // summary:
5  //    This module defines the dojo JSON API.
6
7dojo.fromJson = function(/*String*/ js){
8        // summary:
9        //              Parses a JavaScript expression and returns a JavaScript value.
10        // description:
11        //              Throws for invalid JavaScript expressions. It does not use a strict JSON parser. It
12        //              always delegates to eval(). The content passed to this method must therefore come
13        //              from a trusted source.
14        //              It is recommend that you use dojo/json's parse function for an
15        //              implementation uses the (faster) native JSON parse when available.
16        // js:
17        //              a string literal of a JavaScript expression, for instance:
18        //                      `'{ "foo": [ "bar", 1, { "baz": "thud" } ] }'`
19
20        return eval("(" + js + ")"); // Object
21};
22
23/*=====
24dojo._escapeString = function(){
25        // summary:
26        //              Adds escape sequences for non-visual characters, double quote and
27        //              backslash and surrounds with double quotes to form a valid string
28        //              literal.
29};
30=====*/
31dojo._escapeString = json.stringify; // just delegate to json.stringify
32
33dojo.toJsonIndentStr = "\t";
34dojo.toJson = function(/*Object*/ it, /*Boolean?*/ prettyPrint){
35        // summary:
36        //              Returns a [JSON](http://json.org) serialization of an object.
37        // description:
38        //              Returns a [JSON](http://json.org) serialization of an object.
39        //              Note that this doesn't check for infinite recursion, so don't do that!
40        //              It is recommend that you use dojo/json's stringify function for an lighter
41        //              and faster implementation that matches the native JSON API and uses the
42        //              native JSON serializer when available.
43        // it:
44        //              an object to be serialized. Objects may define their own
45        //              serialization via a special "__json__" or "json" function
46        //              property. If a specialized serializer has been defined, it will
47        //              be used as a fallback.
48        //              Note that in 1.6, toJson would serialize undefined, but this no longer supported
49        //              since it is not supported by native JSON serializer.
50        // prettyPrint:
51        //              if true, we indent objects and arrays to make the output prettier.
52        //              The variable `dojo.toJsonIndentStr` is used as the indent string --
53        //              to use something other than the default (tab), change that variable
54        //              before calling dojo.toJson().
55        //              Note that if native JSON support is available, it will be used for serialization,
56        //              and native implementations vary on the exact spacing used in pretty printing.
57        // returns:
58        //              A JSON string serialization of the passed-in object.
59        // example:
60        //              simple serialization of a trivial object
61        //              |       var jsonStr = dojo.toJson({ howdy: "stranger!", isStrange: true });
62        //              |       doh.is('{"howdy":"stranger!","isStrange":true}', jsonStr);
63        // example:
64        //              a custom serializer for an objects of a particular class:
65        //              |       dojo.declare("Furby", null, {
66        //              |               furbies: "are strange",
67        //              |               furbyCount: 10,
68        //              |               __json__: function(){
69        //              |               },
70        //              |       });
71
72        // use dojo/json
73        return json.stringify(it, function(key, value){
74                if(value){
75                        var tf = value.__json__||value.json;
76                        if(typeof tf == "function"){
77                                return tf.call(value);
78                        }
79                }
80                return value;
81        }, prettyPrint && dojo.toJsonIndentStr);        // String
82};
83
84return dojo;
85});
Note: See TracBrowser for help on using the repository browser.