source: [view]
if(it === undefined){
return "undefined";
}
var objtype = typeof it;
if(objtype == "number" || objtype == "boolean"){
return it + "";
}
if(it === null){
return "null";
}
if(dojo.isString(it)){
return dojo._escapeString(it);
}
// recurse
var recurse = arguments.callee;
// short-circuit for objects that support "json" serialization
// if they return "self" then just pass-through...
var newObj;
_indentStr = _indentStr || "";
var nextIndent = prettyPrint ? _indentStr + dojo.toJsonIndentStr : "";
var tf = it.__json__||it.json;
if(dojo.isFunction(tf)){
newObj = tf.call(it);
if(it !== newObj){
return recurse(newObj, prettyPrint, nextIndent);
}
}
if(it.nodeType && it.cloneNode){ // isNode
// we can't seriailize DOM nodes as regular objects because they have cycles
// DOM nodes could be serialized with something like outerHTML, but
// that can be provided by users in the form of .json or .__json__ function.
throw new Error("Can't serialize DOM nodes");
}
var sep = prettyPrint ? " " : "";
var newLine = prettyPrint ? "\n" : "";
// array
if(dojo.isArray(it)){
var res = dojo.map(it, function(obj){
var val = recurse(obj, prettyPrint, nextIndent);
if(typeof val != "string"){
val = "undefined";
}
return newLine + nextIndent + val;
});
return "[" + res.join("," + sep) + newLine + _indentStr + "]";
}
/*
// look in the registry
try {
window.o = it;
newObj = dojo.json.jsonRegistry.match(it);
return recurse(newObj, prettyPrint, nextIndent);
}catch(e){
// console.log(e);
}
// it's a function with no adapter, skip it
*/
if(objtype == "function"){
return null; // null
}
// generic object code path
var output = [], key;
for(key in it){
var keyStr, val;
if(typeof key == "number"){
keyStr = '"' + key + '"';
}else if(typeof key == "string"){
keyStr = dojo._escapeString(key);
}else{
// skip non-string or number keys
continue;
}
val = recurse(it[key], prettyPrint, nextIndent);
if(typeof val != "string"){
// skip non-serializable values
continue;
}
// FIXME: use += on Moz!!
// MOW NOTE: using += is a pain because you have to account for the dangling comma...
output.push(newLine + nextIndent + keyStr + ":" + sep + val);
}
return "{" + output.join("," + sep) + newLine + _indentStr + "}"; // String