Skip to content

Instantly share code, notes, and snippets.

@fwextensions
Created May 16, 2012 21:04
Show Gist options
  • Save fwextensions/2713933 to your computer and use it in GitHub Desktop.
Save fwextensions/2713933 to your computer and use it in GitHub Desktop.
dojo in FW JS
// embed a local copy of the dojo JSON library, which has been changed
// to not depend on any other part of dojo. that way, we don't have to
// rely on external libraries. unlike Crockford's library, the dojo
// implementation doesn't change the prototypes of basic types, which
// caused problems for the Path panel, and possibly others.
var dojo = {};
dojo.fromJson = function(/*String*/ json){
return eval("(" + json + ")"); // Object
}
dojo._escapeString = function(/*String*/str){
return ('"' + str.replace(/(["\\])/g, '\\$1') + '"').
replace(/[\f]/g, "\\f").replace(/[\b]/g, "\\b").replace(/[\n]/g, "\\n").
replace(/[\t]/g, "\\t").replace(/[\r]/g, "\\r"); // string
}
dojo.toJsonIndentStr = "\t";
dojo.toJson = function(/*Object*/ it, /*Boolean?*/ prettyPrint, /*String?*/ _indentStr){
if(it === undefined){
// dojo was incorrectly returning "undefined" here
return undefined;
}
var objtype = typeof it;
if(objtype == "number" || objtype == "boolean"){
return it + "";
}
if(it === null){
return "null";
}
if(typeof it == "string" || it instanceof String){
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(typeof tf == "function"){
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(it && it instanceof Array){
var res = [];
for (var i = 0, len = it.length; i < len; i++) {
var val = recurse(it[i], prettyPrint, nextIndent);
if(typeof val != "string"){
// dojo was incorrectly returning "undefined" here
val = undefined;
}
res[i] = newLine + nextIndent + val;
}
return "[" + res.join("," + sep) + newLine + _indentStr + "]";
}
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment