summaryrefslogtreecommitdiff
path: root/lib/dojo/_base/json.js
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2011-03-04 19:02:28 +0300
committerAndrew Dolgov <[email protected]>2011-03-04 19:02:59 +0300
commita089699c8915636ba4f158d77dba9b012bc93208 (patch)
treeb2d7d051f1f55d44a6be07d3ee137e5a7ccfcefb /lib/dojo/_base/json.js
parentcfad9259a6feacfa8194b1312770ae6db1ecce50 (diff)
build custom layer of Dojo to speed up loading of tt-rss (refs #293)
Diffstat (limited to 'lib/dojo/_base/json.js')
-rw-r--r--lib/dojo/_base/json.js211
1 files changed, 142 insertions, 69 deletions
diff --git a/lib/dojo/_base/json.js b/lib/dojo/_base/json.js
index 7d8c5af65..4d50400c9 100644
--- a/lib/dojo/_base/json.js
+++ b/lib/dojo/_base/json.js
@@ -5,77 +5,150 @@
*/
-if(!dojo._hasResource["dojo._base.json"]){
-dojo._hasResource["dojo._base.json"]=true;
+if(!dojo._hasResource["dojo._base.json"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
+dojo._hasResource["dojo._base.json"] = true;
dojo.provide("dojo._base.json");
-dojo.fromJson=function(_1){
-return eval("("+_1+")");
-};
-dojo._escapeString=function(_2){
-return ("\""+_2.replace(/(["\\])/g,"\\$1")+"\"").replace(/[\f]/g,"\\f").replace(/[\b]/g,"\\b").replace(/[\n]/g,"\\n").replace(/[\t]/g,"\\t").replace(/[\r]/g,"\\r");
-};
-dojo.toJsonIndentStr="\t";
-dojo.toJson=function(it,_3,_4){
-if(it===undefined){
-return "undefined";
-}
-var _5=typeof it;
-if(_5=="number"||_5=="boolean"){
-return it+"";
-}
-if(it===null){
-return "null";
-}
-if(dojo.isString(it)){
-return dojo._escapeString(it);
-}
-var _6=arguments.callee;
-var _7;
-_4=_4||"";
-var _8=_3?_4+dojo.toJsonIndentStr:"";
-var tf=it.__json__||it.json;
-if(dojo.isFunction(tf)){
-_7=tf.call(it);
-if(it!==_7){
-return _6(_7,_3,_8);
-}
-}
-if(it.nodeType&&it.cloneNode){
-throw new Error("Can't serialize DOM nodes");
-}
-var _9=_3?" ":"";
-var _a=_3?"\n":"";
-if(dojo.isArray(it)){
-var _b=dojo.map(it,function(_c){
-var _d=_6(_c,_3,_8);
-if(typeof _d!="string"){
-_d="undefined";
-}
-return _a+_8+_d;
-});
-return "["+_b.join(","+_9)+_a+_4+"]";
-}
-if(_5=="function"){
-return null;
-}
-var _e=[],_f;
-for(_f in it){
-var _10,val;
-if(typeof _f=="number"){
-_10="\""+_f+"\"";
-}else{
-if(typeof _f=="string"){
-_10=dojo._escapeString(_f);
-}else{
-continue;
-}
+
+dojo.fromJson = function(/*String*/ json){
+ // summary:
+ // Parses a [JSON](http://json.org) string to return a JavaScript object.
+ // description:
+ // Throws for invalid JSON strings, but it does not use a strict JSON parser. It
+ // delegates to eval(). The content passed to this method must therefore come
+ // from a trusted source.
+ // json:
+ // a string literal of a JSON item, for instance:
+ // `'{ "foo": [ "bar", 1, { "baz": "thud" } ] }'`
+
+ return eval("(" + json + ")"); // Object
}
-val=_6(it[_f],_3,_8);
-if(typeof val!="string"){
-continue;
+
+dojo._escapeString = function(/*String*/str){
+ //summary:
+ // Adds escape sequences for non-visual characters, double quote and
+ // backslash and surrounds with double quotes to form a valid string
+ // literal.
+ 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
}
-_e.push(_a+_8+_10+":"+_9+val);
+
+dojo.toJsonIndentStr = "\t";
+dojo.toJson = function(/*Object*/ it, /*Boolean?*/ prettyPrint, /*String?*/ _indentStr){
+ // summary:
+ // Returns a [JSON](http://json.org) serialization of an object.
+ // description:
+ // Returns a [JSON](http://json.org) serialization of an object.
+ // Note that this doesn't check for infinite recursion, so don't do that!
+ // it:
+ // an object to be serialized. Objects may define their own
+ // serialization via a special "__json__" or "json" function
+ // property. If a specialized serializer has been defined, it will
+ // be used as a fallback.
+ // prettyPrint:
+ // if true, we indent objects and arrays to make the output prettier.
+ // The variable `dojo.toJsonIndentStr` is used as the indent string --
+ // to use something other than the default (tab), change that variable
+ // before calling dojo.toJson().
+ // _indentStr:
+ // private variable for recursive calls when pretty printing, do not use.
+ // example:
+ // simple serialization of a trivial object
+ // | var jsonStr = dojo.toJson({ howdy: "stranger!", isStrange: true });
+ // | doh.is('{"howdy":"stranger!","isStrange":true}', jsonStr);
+ // example:
+ // a custom serializer for an objects of a particular class:
+ // | dojo.declare("Furby", null, {
+ // | furbies: "are strange",
+ // | furbyCount: 10,
+ // | __json__: function(){
+ // | },
+ // | });
+
+ 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
}
-return "{"+_e.join(","+_9)+_a+_4+"}";
-};
+
}