Changeset 7750
- Timestamp:
- 03/22/07 09:10:22 (22 months ago)
- Location:
- dojo/trunk
- Files:
-
- 1 added
- 2 modified
-
tests/_base.js (modified) (1 diff)
-
tests/_base/json.js (added)
-
_base/json.js (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
dojo/trunk/tests/_base.js
r7726 r7750 119 119 dojo.require("tests._base.lang"); 120 120 dojo.require("tests._base.Deferred"); 121 dojo.require("tests._base.json"); 121 122 // FIXME: add test includes for the rest of the Dojo Base groups here 122 123 }catch(e){ -
dojo/trunk/_base/json.js
r7731 r7750 32 32 } 33 33 34 dojo.toJson = function(/*Object*/ obj){ 34 dojo.toJsonIndentStr = "\t"; 35 dojo.toJson = function(/*Object*/ it, /* Boolean */ prettyPrint, /* String */ _indentStr){ 35 36 // summary: 36 // Create a JSON serialization of an object, note that this doesn't 37 // check for infinite recursion, so don't do that! 38 // obj: 37 // Create a JSON serialization of an object. 38 // Note that this doesn't check for infinite recursion, so don't do that! 39 // 40 // it: 39 41 // an object to be serialized. Objects may define their own 40 42 // serialization via a special "__json__" or "json" function 41 43 // property. If a specialized serializer has been defined, it will 42 44 // be used as a fallback. 45 // 46 // prettyPrint: 47 // if true, we indent objects and arrays to make the output prettier. 48 // The variable dojo.toJsonIndentStr is used as the indent string 49 // -- to use something other than the default (tab), 50 // change that variable before calling dojo.toJson(). 51 // 52 // _indentStr: 53 // private variable for recursive calls when pretty printing, do not use. 54 // 43 55 // return: 44 // a String representing the serialized version of the passed 45 // object 56 // a String representing the serialized version of the passed object. 46 57 47 var o = obj; 48 var objtype = typeof(o); 58 _indentStr = _indentStr || ""; 59 var nextIndent = (prettyPrint ? _indentStr + dojo.toJsonIndentStr : ""); 60 var newLine = (prettyPrint ? "\n" : ""); 61 var objtype = typeof(it); 49 62 if(objtype == "undefined"){ 50 63 return "undefined"; 51 64 }else if((objtype == "number")||(objtype == "boolean")){ 52 return o+ "";53 }else if( o=== null){65 return it + ""; 66 }else if(it === null){ 54 67 return "null"; 55 68 } 56 if(objtype == "string"){ return dojo._escapeString( o); }69 if(objtype == "string"){ return dojo._escapeString(it); } 57 70 // recurse 58 var me = arguments.callee;71 var recurse = arguments.callee; 59 72 // short-circuit for objects that support "json" serialization 60 73 // if they return "self" then just pass-through... 61 74 var newObj; 62 if(typeof o.__json__ == "function"){63 newObj = o.__json__();64 if( o!== newObj){65 return me(newObj);75 if(typeof it.__json__ == "function"){ 76 newObj = it.__json__(); 77 if(it !== newObj){ 78 return recurse(newObj, prettyPrint, nextIndent); 66 79 } 67 80 } 68 if(typeof o.json == "function"){69 newObj = o.json();70 if( o!== newObj){71 return me(newObj);81 if(typeof it.json == "function"){ 82 newObj = it.json(); 83 if(it !== newObj){ 84 return recurse(newObj, prettyPrint, nextIndent); 72 85 } 73 86 } 74 87 // array 75 if(dojo.isArray( o)){88 if(dojo.isArray(it)){ 76 89 var res = []; 77 for(var i = 0; i < o.length; i++){78 var val = me(o[i]);90 for(var i = 0; i < it.length; i++){ 91 var val = recurse(it[i], prettyPrint, nextIndent); 79 92 if(typeof(val) != "string"){ 80 93 val = "undefined"; 81 94 } 82 res.push( val);95 res.push(newLine + nextIndent + val); 83 96 } 84 return "[" + res.join(",") + "]";97 return "[" + res.join(",") + newLine + _indentStr + "]"; 85 98 } 86 99 /* 87 100 // look in the registry 88 101 try { 89 window.o = o;90 newObj = dojo.json.jsonRegistry.match( o);91 return me(newObj);102 window.o = it; 103 newObj = dojo.json.jsonRegistry.match(it); 104 return recurse(newObj, prettyPrint, nextIndent); 92 105 }catch(e){ 93 106 // console.debug(e); 94 107 } 95 // it's a function with no adapter, bad108 // it's a function with no adapter, skip it 96 109 */ 97 110 if(objtype == "function"){ … … 99 112 } 100 113 // generic object code path 101 res= [];102 for(var k in o){103 var useKey;104 if(typeof(k ) == "number"){105 useKey = '"' + k+ '"';106 }else if(typeof(k ) == "string"){107 useKey = dojo._escapeString(k);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); 108 121 }else{ 109 122 // skip non-string or number keys 110 123 continue; 111 124 } 112 val = me(o[k]);125 val = recurse(it[key], prettyPrint, nextIndent); 113 126 if(typeof(val) != "string"){ 114 127 // skip non-serializable values … … 116 129 } 117 130 // FIXME: use += on Moz!! 118 res.push(useKey + ":" + val); 131 // MOW NOTE: using += is a pain because you have to account for the dangling comma... 132 output.push(newLine + nextIndent + keyStr + ":" + val); 119 133 } 120 return "{" + res.join(",")+ "}";134 return "{" + output.join(",") + newLine + _indentStr + "}"; 121 135 }