forked from meritt/base2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase2-json-p.js
9 lines (8 loc) · 2.06 KB
/
base2-json-p.js
1
2
3
4
5
6
7
8
9
/*
base2 - copyright 2007-2009, Dean Edwards
http://code.google.com/p/base2/
http://www.opensource.org/licenses/mit-license.php
Contributors:
Doeke Zanstra
*/
new function(_0){var JSON=new base2.Package(this,{name:"JSON",imports:"Enumerable",version:"0.9",VALID:/^("(\\.|[^"\\\n\r])*"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])*$/,copy:function(a){return this.parse(this.toString(a))},parse:function(a){return this.String.parseJSON(a)}});eval(this.imports);JSON.toString=function(c){if(arguments.length==0)return"[base2.JSON]";var d=this.Object;try{forEach(this,function(a,b){if(JSON.Object.ancestorOf(a)&&instanceOf(c,global[b])){d=a;throw StopIteration;}})}catch(error){if(error!=StopIteration)throw error;}return d.toJSONString(c)};function pad(a,b){return"0000".slice(0,(b||2)-String(a).length)+a};JSON.Object=Module.extend({toJSONString:function(d){return d==null?"null":"{"+reduce(d,function(a,b,c){if(JSON.Object.isValid(b)){a.push(JSON.String.toJSONString(c)+":"+JSON.toString(b))}return a},[]).join(",")+"}"}},{VALID_TYPE:/^(object|boolean|number|string)$/,isValid:function(a){return this.VALID_TYPE.test(typeof a)}});JSON.Array=JSON.Object.extend({toJSONString:function(a){var b=a.length,c=[];while(b--)c[b]=JSON.Object.isValid(a[b])?JSON.toString(a[b]):"null";return"["+c.join(",")+"]"}});JSON.Boolean=JSON.Object.extend({toJSONString:function(a){return String(a)}});JSON.Date=JSON.Object.extend({toJSONString:function(a){return'"'+Date2.toISOString(a)+'"'}});JSON.Number=JSON.Object.extend({toJSONString:function(a){return isFinite(a)?String(a):"null"}});JSON.String=JSON.Object.extend({parseJSON:function(a){try{if(JSON.VALID.test(a)){return new Function("return "+a)()}}catch(x){throw new SyntaxError("parseJSON");}return""},toJSONString:function(a){return'"'+this.ESCAPE.exec(a)+'"'}},{ESCAPE:new RegGrp({'"':'\\"','\\\\':'\\\\'})});JSON.String.ESCAPE.put(/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/,function(a){var b=a.charCodeAt(0);return'\\u00'+(~~(b/16)).toString(16)+(b%16).toString(16)});eval(this.exports)};