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