json2.js 2.7 KB

1
  1. "object"!==typeof JSON&&(JSON={});(function(){function k(a){return 10>a?"0"+a:a}function n(){return this.valueOf()}function p(a){q.lastIndex=0;return q.test(a)?'"'+a.replace(q,function(a){var c=u[a];return"string"===typeof c?c:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function l(a,c){var f=d,b=c[a];b&&"object"===typeof b&&"function"===typeof b.toJSON&&(b=b.toJSON(a));"function"===typeof h&&(b=h.call(c,a,b));switch(typeof b){case "string":return p(b);case "number":return isFinite(b)?String(b):"null";case "boolean":case "null":return String(b);case "object":if(!b)return"null";d+=m;var g=[];if("[object Array]"===Object.prototype.toString.apply(b)){var r=b.length;for(a=0;a<r;a+=1)g[a]=l(a,b)||"null";c=0===g.length?"[]":d?"[\n"+d+g.join(",\n"+d)+"\n"+f+"]":"["+g.join(",")+"]";d=f;return c}if(h&&"object"===typeof h)for(r=h.length,a=0;a<r;a+=1){if("string"===typeof h[a]){var e=h[a];(c=l(e,b))&&g.push(p(e)+(d?": ":":")+c)}}else for(e in b)Object.prototype.hasOwnProperty.call(b,e)&&(c=l(e,b))&&g.push(p(e)+(d?": ":":")+c);c=0===g.length?"{}":d?"{\n"+d+g.join(",\n"+d)+"\n"+f+"}":"{"+g.join(",")+"}";d=f;return c}}var v=/^[\],:{}\s]*$/,w=/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,x=/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,y=/(?:^|:|,)(?:\s*\[)+/g,q=/[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,t=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;"function"!==typeof Date.prototype.toJSON&&(Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+k(this.getUTCMonth()+1)+"-"+k(this.getUTCDate())+"T"+k(this.getUTCHours())+":"+k(this.getUTCMinutes())+":"+k(this.getUTCSeconds())+"Z":null},Boolean.prototype.toJSON=n,Number.prototype.toJSON=n,String.prototype.toJSON=n);var d,m,h;if("function"!==typeof JSON.stringify){var u={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"};JSON.stringify=function(a,c,f){var b;m=d="";if("number"===typeof f)for(b=0;b<f;b+=1)m+=" ";else"string"===typeof f&&(m=f);if((h=c)&&"function"!==typeof c&&("object"!==typeof c||"number"!==typeof c.length))throw Error("JSON.stringify");return l("",{"":a})}}"function"!==typeof JSON.parse&&(JSON.parse=function(a,c){function d(a,g){var b,e=a[g];if(e&&"object"===typeof e)for(b in e)if(Object.prototype.hasOwnProperty.call(e,b)){var f=d(e,b);void 0!==f?e[b]=f:delete e[b]}return c.call(a,g,e)}a=String(a);t.lastIndex=0;t.test(a)&&(a=a.replace(t,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)}));if(v.test(a.replace(w,"@").replace(x,"]").replace(y,"")))return a=eval("("+a+")"),"function"===typeof c?d({"":a},""):a;throw new SyntaxError("JSON.parse");})})();