]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/_base/json.js
Comment class stub
[eow] / static / dojo-release-1.1.1 / dojo / _base / json.js
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");
4
5 dojo.fromJson = function(/*String*/ json){
6         // summary:
7         //              Parses a [JSON](http://json.org) string to return a JavaScript object.
8         // json: 
9         //              a string literal of a JSON item, for instance:
10         //                      `'{ "foo": [ "bar", 1, { "baz": "thud" } ] }'`
11
12         return eval("(" + json + ")"); // Object
13 }
14
15 dojo._escapeString = function(/*String*/str){
16         //summary:
17         //              Adds escape sequences for non-visual characters, double quote and
18         //              backslash and surrounds with double quotes to form a valid string
19         //              literal.
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
23 }
24
25 dojo.toJsonIndentStr = "\t";
26 dojo.toJson = function(/*Object*/ it, /*Boolean?*/ prettyPrint, /*String?*/ _indentStr){
27         // summary:
28         //              Returns a [JSON](http://json.org) serialization of an object.
29         //
30         // description:
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!
33         //
34         // it:
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.
39         //
40         // prettyPrint:
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().
45         //
46         // _indentStr:
47         //              private variable for recursive calls when pretty printing, do not use.
48
49         if(it === undefined){
50                 return "undefined";
51         }
52         var objtype = typeof it;
53         if(objtype == "number" || objtype == "boolean"){
54                 return it + "";
55         }
56         if(it === null){
57                 return "null";
58         }
59         if(dojo.isString(it)){ 
60                 return dojo._escapeString(it); 
61         }
62         if(it.nodeType && it.cloneNode){ // isNode
63                 return ""; // FIXME: would something like outerHTML be better here?
64         }
65         // recurse
66         var recurse = arguments.callee;
67         // short-circuit for objects that support "json" serialization
68         // if they return "self" then just pass-through...
69         var newObj;
70         _indentStr = _indentStr || "";
71         var nextIndent = prettyPrint ? _indentStr + dojo.toJsonIndentStr : "";
72         if(typeof it.__json__ == "function"){
73                 newObj = it.__json__();
74                 if(it !== newObj){
75                         return recurse(newObj, prettyPrint, nextIndent);
76                 }
77         }
78         if(typeof it.json == "function"){
79                 newObj = it.json();
80                 if(it !== newObj){
81                         return recurse(newObj, prettyPrint, nextIndent);
82                 }
83         }
84
85         var sep = prettyPrint ? " " : "";
86         var newLine = prettyPrint ? "\n" : "";
87
88         // array
89         if(dojo.isArray(it)){
90                 var res = dojo.map(it, function(obj){
91                         var val = recurse(obj, prettyPrint, nextIndent);
92                         if(typeof val != "string"){
93                                 val = "undefined";
94                         }
95                         return newLine + nextIndent + val;
96                 });
97                 return "[" + res.join("," + sep) + newLine + _indentStr + "]";
98         }
99         /*
100         // look in the registry
101         try {
102                 window.o = it;
103                 newObj = dojo.json.jsonRegistry.match(it);
104                 return recurse(newObj, prettyPrint, nextIndent);
105         }catch(e){
106                 // console.debug(e);
107         }
108         // it's a function with no adapter, skip it
109         */
110         if(objtype == "function"){
111                 return null; // null
112         }
113         // generic object code path
114         var output = [];
115         for(var key in it){
116                 var keyStr;
117                 if(typeof key == "number"){
118                         keyStr = '"' + key + '"';
119                 }else if(typeof key == "string"){
120                         keyStr = dojo._escapeString(key);
121                 }else{
122                         // skip non-string or number keys
123                         continue;
124                 }
125                 val = recurse(it[key], prettyPrint, nextIndent);
126                 if(typeof val != "string"){
127                         // skip non-serializable values
128                         continue;
129                 }
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);
133         }
134         return "{" + output.join("," + sep) + newLine + _indentStr + "}"; // String
135 }
136
137 }