Changeset 7750

Show
Ignore:
Timestamp:
03/22/07 09:10:22 (22 months ago)
Author:
owen
Message:

fixes #2629 -- ads prettyPrint option to dojo.toJson, added JSON tests to harness

Location:
dojo/trunk
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • dojo/trunk/tests/_base.js

    r7726 r7750  
    119119        dojo.require("tests._base.lang"); 
    120120        dojo.require("tests._base.Deferred"); 
     121        dojo.require("tests._base.json"); 
    121122        // FIXME: add test includes for the rest of the Dojo Base groups here 
    122123}catch(e){ 
  • dojo/trunk/_base/json.js

    r7731 r7750  
    3232} 
    3333 
    34 dojo.toJson = function(/*Object*/ obj){ 
     34dojo.toJsonIndentStr = "\t"; 
     35dojo.toJson = function(/*Object*/ it, /* Boolean */ prettyPrint, /* String */ _indentStr){ 
    3536        // 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: 
    3941        //              an object to be serialized. Objects may define their own 
    4042        //              serialization via a special "__json__" or "json" function 
    4143        //              property. If a specialized serializer has been defined, it will 
    4244        //              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        //               
    4355        // return: 
    44         //              a String representing the serialized version of the passed 
    45         //              object 
     56        //              a String representing the serialized version of the passed object. 
    4657 
    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); 
    4962        if(objtype == "undefined"){ 
    5063                return "undefined"; 
    5164        }else if((objtype == "number")||(objtype == "boolean")){ 
    52                 return o + ""; 
    53         }else if(o === null){ 
     65                return it + ""; 
     66        }else if(it === null){ 
    5467                return "null"; 
    5568        } 
    56         if(objtype == "string"){ return dojo._escapeString(o); } 
     69        if(objtype == "string"){ return dojo._escapeString(it); } 
    5770        // recurse 
    58         var me = arguments.callee; 
     71        var recurse = arguments.callee; 
    5972        // short-circuit for objects that support "json" serialization 
    6073        // if they return "self" then just pass-through... 
    6174        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); 
    6679                } 
    6780        } 
    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); 
    7285                } 
    7386        } 
    7487        // array 
    75         if(dojo.isArray(o)){ 
     88        if(dojo.isArray(it)){ 
    7689                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); 
    7992                        if(typeof(val) != "string"){ 
    8093                                val = "undefined"; 
    8194                        } 
    82                         res.push(val); 
     95                        res.push(newLine + nextIndent + val); 
    8396                } 
    84                 return "[" + res.join(",") + "]"; 
     97                return "[" + res.join(",") + newLine + _indentStr + "]"; 
    8598        } 
    8699        /* 
    87100        // look in the registry 
    88101        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); 
    92105        }catch(e){ 
    93106                // console.debug(e); 
    94107        } 
    95         // it's a function with no adapter, bad 
     108        // it's a function with no adapter, skip it 
    96109        */ 
    97110        if(objtype == "function"){ 
     
    99112        } 
    100113        // 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); 
    108121                }else{ 
    109122                        // skip non-string or number keys 
    110123                        continue; 
    111124                } 
    112                 val = me(o[k]); 
     125                val = recurse(it[key], prettyPrint, nextIndent); 
    113126                if(typeof(val) != "string"){ 
    114127                        // skip non-serializable values 
     
    116129                } 
    117130                // 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); 
    119133        } 
    120         return "{" + res.join(",") + "}"; 
     134        return "{" + output.join(",") + newLine + _indentStr + "}"; 
    121135}