1 if(!dojo._hasResource["dojo._base.json"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo._base.json"] = true;
3 dojo.provide("dojo._base.json");
5 dojo.fromJson = function(/*String*/ json){
7 // Parses a [JSON](http://json.org) string to return a JavaScript object.
9 // a string literal of a JSON item, for instance:
10 // `'{ "foo": [ "bar", 1, { "baz": "thud" } ] }'`
12 return eval("(" + json + ")"); // Object
15 dojo._escapeString = function(/*String*/str){
17 // Adds escape sequences for non-visual characters, double quote and
18 // backslash and surrounds with double quotes to form a valid string
20 return ('"' + str.replace(/(["\\])/g, '\\$1') + '"').
21 replace(/[\f]/g, "\\f").replace(/[\b]/g, "\\b").replace(/[\n]/g, "\\n").
22 replace(/[\t]/g, "\\t").replace(/[\r]/g, "\\r"); // string
25 dojo.toJsonIndentStr = "\t";
26 dojo.toJson = function(/*Object*/ it, /*Boolean?*/ prettyPrint, /*String?*/ _indentStr){
28 // Returns a [JSON](http://json.org) serialization of an object.
31 // Returns a [JSON](http://json.org) serialization of an object.
32 // Note that this doesn't check for infinite recursion, so don't do that!
35 // an object to be serialized. Objects may define their own
36 // serialization via a special "__json__" or "json" function
37 // property. If a specialized serializer has been defined, it will
38 // be used as a fallback.
41 // if true, we indent objects and arrays to make the output prettier.
42 // The variable dojo.toJsonIndentStr is used as the indent string
43 // -- to use something other than the default (tab),
44 // change that variable before calling dojo.toJson().
47 // private variable for recursive calls when pretty printing, do not use.
52 var objtype = typeof it;
53 if(objtype == "number" || objtype == "boolean"){
59 if(dojo.isString(it)){
60 return dojo._escapeString(it);
62 if(it.nodeType && it.cloneNode){ // isNode
63 return ""; // FIXME: would something like outerHTML be better here?
66 var recurse = arguments.callee;
67 // short-circuit for objects that support "json" serialization
68 // if they return "self" then just pass-through...
70 _indentStr = _indentStr || "";
71 var nextIndent = prettyPrint ? _indentStr + dojo.toJsonIndentStr : "";
72 if(typeof it.__json__ == "function"){
73 newObj = it.__json__();
75 return recurse(newObj, prettyPrint, nextIndent);
78 if(typeof it.json == "function"){
81 return recurse(newObj, prettyPrint, nextIndent);
85 var sep = prettyPrint ? " " : "";
86 var newLine = prettyPrint ? "\n" : "";
90 var res = dojo.map(it, function(obj){
91 var val = recurse(obj, prettyPrint, nextIndent);
92 if(typeof val != "string"){
95 return newLine + nextIndent + val;
97 return "[" + res.join("," + sep) + newLine + _indentStr + "]";
100 // look in the registry
103 newObj = dojo.json.jsonRegistry.match(it);
104 return recurse(newObj, prettyPrint, nextIndent);
108 // it's a function with no adapter, skip it
110 if(objtype == "function"){
113 // generic object code path
117 if(typeof key == "number"){
118 keyStr = '"' + key + '"';
119 }else if(typeof key == "string"){
120 keyStr = dojo._escapeString(key);
122 // skip non-string or number keys
125 val = recurse(it[key], prettyPrint, nextIndent);
126 if(typeof val != "string"){
127 // skip non-serializable values
130 // FIXME: use += on Moz!!
131 // MOW NOTE: using += is a pain because you have to account for the dangling comma...
132 output.push(newLine + nextIndent + keyStr + ":" + sep + val);
134 return "{" + output.join("," + sep) + newLine + _indentStr + "}"; // String