]>
Commit | Line | Data |
---|---|---|
2f01fe57 | 1 | /* |
81bea17a | 2 | Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved. |
2f01fe57 AD |
3 | Available via Academic Free License >= 2.1 OR the modified BSD license. |
4 | see: http://dojotoolkit.org/license for details | |
5 | */ | |
6 | ||
7 | ||
a089699c AD |
8 | if(!dojo._hasResource["dojo._base.json"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
9 | dojo._hasResource["dojo._base.json"] = true; | |
2f01fe57 | 10 | dojo.provide("dojo._base.json"); |
a089699c | 11 | |
81bea17a | 12 | |
a089699c AD |
13 | dojo.fromJson = function(/*String*/ json){ |
14 | // summary: | |
15 | // Parses a [JSON](http://json.org) string to return a JavaScript object. | |
16 | // description: | |
17 | // Throws for invalid JSON strings, but it does not use a strict JSON parser. It | |
18 | // delegates to eval(). The content passed to this method must therefore come | |
19 | // from a trusted source. | |
81bea17a | 20 | // json: |
a089699c AD |
21 | // a string literal of a JSON item, for instance: |
22 | // `'{ "foo": [ "bar", 1, { "baz": "thud" } ] }'` | |
23 | ||
24 | return eval("(" + json + ")"); // Object | |
81bea17a | 25 | }; |
a089699c AD |
26 | |
27 | dojo._escapeString = function(/*String*/str){ | |
28 | //summary: | |
29 | // Adds escape sequences for non-visual characters, double quote and | |
30 | // backslash and surrounds with double quotes to form a valid string | |
31 | // literal. | |
32 | return ('"' + str.replace(/(["\\])/g, '\\$1') + '"'). | |
33 | replace(/[\f]/g, "\\f").replace(/[\b]/g, "\\b").replace(/[\n]/g, "\\n"). | |
34 | replace(/[\t]/g, "\\t").replace(/[\r]/g, "\\r"); // string | |
81bea17a | 35 | }; |
a089699c AD |
36 | |
37 | dojo.toJsonIndentStr = "\t"; | |
38 | dojo.toJson = function(/*Object*/ it, /*Boolean?*/ prettyPrint, /*String?*/ _indentStr){ | |
39 | // summary: | |
40 | // Returns a [JSON](http://json.org) serialization of an object. | |
41 | // description: | |
42 | // Returns a [JSON](http://json.org) serialization of an object. | |
43 | // Note that this doesn't check for infinite recursion, so don't do that! | |
44 | // it: | |
45 | // an object to be serialized. Objects may define their own | |
46 | // serialization via a special "__json__" or "json" function | |
47 | // property. If a specialized serializer has been defined, it will | |
48 | // be used as a fallback. | |
49 | // prettyPrint: | |
50 | // if true, we indent objects and arrays to make the output prettier. | |
51 | // The variable `dojo.toJsonIndentStr` is used as the indent string -- | |
52 | // to use something other than the default (tab), change that variable | |
53 | // before calling dojo.toJson(). | |
54 | // _indentStr: | |
55 | // private variable for recursive calls when pretty printing, do not use. | |
56 | // example: | |
57 | // simple serialization of a trivial object | |
58 | // | var jsonStr = dojo.toJson({ howdy: "stranger!", isStrange: true }); | |
59 | // | doh.is('{"howdy":"stranger!","isStrange":true}', jsonStr); | |
60 | // example: | |
61 | // a custom serializer for an objects of a particular class: | |
62 | // | dojo.declare("Furby", null, { | |
63 | // | furbies: "are strange", | |
64 | // | furbyCount: 10, | |
65 | // | __json__: function(){ | |
66 | // | }, | |
67 | // | }); | |
68 | ||
69 | if(it === undefined){ | |
70 | return "undefined"; | |
71 | } | |
72 | var objtype = typeof it; | |
73 | if(objtype == "number" || objtype == "boolean"){ | |
74 | return it + ""; | |
75 | } | |
76 | if(it === null){ | |
77 | return "null"; | |
78 | } | |
81bea17a AD |
79 | if(dojo.isString(it)){ |
80 | return dojo._escapeString(it); | |
a089699c AD |
81 | } |
82 | // recurse | |
83 | var recurse = arguments.callee; | |
84 | // short-circuit for objects that support "json" serialization | |
85 | // if they return "self" then just pass-through... | |
86 | var newObj; | |
87 | _indentStr = _indentStr || ""; | |
88 | var nextIndent = prettyPrint ? _indentStr + dojo.toJsonIndentStr : ""; | |
89 | var tf = it.__json__||it.json; | |
90 | if(dojo.isFunction(tf)){ | |
91 | newObj = tf.call(it); | |
92 | if(it !== newObj){ | |
93 | return recurse(newObj, prettyPrint, nextIndent); | |
94 | } | |
95 | } | |
96 | if(it.nodeType && it.cloneNode){ // isNode | |
97 | // we can't seriailize DOM nodes as regular objects because they have cycles | |
98 | // DOM nodes could be serialized with something like outerHTML, but | |
99 | // that can be provided by users in the form of .json or .__json__ function. | |
100 | throw new Error("Can't serialize DOM nodes"); | |
101 | } | |
102 | ||
103 | var sep = prettyPrint ? " " : ""; | |
104 | var newLine = prettyPrint ? "\n" : ""; | |
105 | ||
106 | // array | |
107 | if(dojo.isArray(it)){ | |
108 | var res = dojo.map(it, function(obj){ | |
109 | var val = recurse(obj, prettyPrint, nextIndent); | |
110 | if(typeof val != "string"){ | |
111 | val = "undefined"; | |
112 | } | |
113 | return newLine + nextIndent + val; | |
114 | }); | |
115 | return "[" + res.join("," + sep) + newLine + _indentStr + "]"; | |
116 | } | |
117 | /* | |
118 | // look in the registry | |
119 | try { | |
120 | window.o = it; | |
121 | newObj = dojo.json.jsonRegistry.match(it); | |
122 | return recurse(newObj, prettyPrint, nextIndent); | |
123 | }catch(e){ | |
124 | // console.log(e); | |
125 | } | |
126 | // it's a function with no adapter, skip it | |
127 | */ | |
128 | if(objtype == "function"){ | |
129 | return null; // null | |
130 | } | |
131 | // generic object code path | |
132 | var output = [], key; | |
133 | for(key in it){ | |
134 | var keyStr, val; | |
135 | if(typeof key == "number"){ | |
136 | keyStr = '"' + key + '"'; | |
137 | }else if(typeof key == "string"){ | |
138 | keyStr = dojo._escapeString(key); | |
139 | }else{ | |
140 | // skip non-string or number keys | |
141 | continue; | |
142 | } | |
143 | val = recurse(it[key], prettyPrint, nextIndent); | |
144 | if(typeof val != "string"){ | |
145 | // skip non-serializable values | |
146 | continue; | |
147 | } | |
148 | // FIXME: use += on Moz!! | |
149 | // MOW NOTE: using += is a pain because you have to account for the dangling comma... | |
150 | output.push(newLine + nextIndent + keyStr + ":" + sep + val); | |
151 | } | |
152 | return "{" + output.join("," + sep) + newLine + _indentStr + "}"; // String | |
81bea17a | 153 | }; |
a089699c | 154 | |
2f01fe57 | 155 | } |