OLD | NEW |
| (Empty) |
1 /* | |
2 JSONstring v 1.02 | |
3 copyright 2006-2010 Thomas Frank | |
4 (Small sanitizer added to the toObject-method, May 2008) | |
5 (Scrungus fix to some problems with quotes in strings added in July 2010) | |
6 | |
7 This EULA grants you the following rights: | |
8 | |
9 Installation and Use. You may install and use an unlimited number of copies of t
he SOFTWARE PRODUCT. | |
10 | |
11 Reproduction and Distribution. You may reproduce and distribute an unlimited num
ber of copies of the SOFTWARE PRODUCT either in whole or in part; each copy shou
ld include all copyright and trademark notices, and shall be accompanied by a co
py of this EULA. Copies of the SOFTWARE PRODUCT may be distributed as a standalo
ne product or included with your own product. | |
12 | |
13 Commercial Use. You may sell for profit and freely distribute scripts and/or com
piled scripts that were created with the SOFTWARE PRODUCT. | |
14 | |
15 Based on Steve Yen's implementation: | |
16 http://trimpath.com/project/wiki/JsonLibrary | |
17 | |
18 Sanitizer regExp: | |
19 Andrea Giammarchi 2007 | |
20 | |
21 */ | |
22 | |
23 JSONstring={ | |
24 compactOutput:false, | |
25 includeProtos:false, | |
26 includeFunctions: false, | |
27 detectCirculars:true, | |
28 restoreCirculars:true, | |
29 make:function(arg,restore) { | |
30 this.restore=restore; | |
31 this.mem=[];this.pathMem=[]; | |
32 return this.toJsonStringArray(arg).join(''); | |
33 }, | |
34 toObject:function(x){ | |
35 if(!this.cleaner){ | |
36 try{this.cleaner=new RegExp('^("(\\\\.|[^"\\\\\\n\\r])*?
"|[,:{}\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t])+?$')} | |
37 catch(a){this.cleaner=/^(true|false|null|\[.*\]|\{.*\}|"
.*"|\d+|\d+\.\d+)$/} | |
38 }; | |
39 if(!this.cleaner.test(x)){return {}}; | |
40 eval("this.myObj="+x); | |
41 if(!this.restoreCirculars || !alert){return this.myObj}; | |
42 if(this.includeFunctions){ | |
43 var x=this.myObj; | |
44 for(var i in x){if(typeof x[i]=="string" && !x[i].indexO
f("JSONincludedFunc:")){ | |
45 x[i]=x[i].substring(17); | |
46 eval("x[i]="+x[i]) | |
47 }} | |
48 }; | |
49 this.restoreCode=[]; | |
50 this.make(this.myObj,true); | |
51 var r=this.restoreCode.join(";")+";"; | |
52 eval('r=r.replace(/\\W([0-9]{1,})(\\W)/g,"[$1]$2").replace(/\\.\
\;/g,";")'); | |
53 eval(r); | |
54 return this.myObj | |
55 }, | |
56 toJsonStringArray:function(arg, out) { | |
57 if(!out){this.path=[]}; | |
58 out = out || []; | |
59 var u; // undefined | |
60 switch (typeof arg) { | |
61 case 'object': | |
62 this.lastObj=arg; | |
63 if(this.detectCirculars){ | |
64 var m=this.mem; var n=this.pathMem; | |
65 for(var i=0;i<m.length;i++){ | |
66 if(arg===m[i]){ | |
67 out.push('"JSONcircRef:'+n[i]+'"
');return out | |
68 } | |
69 }; | |
70 m.push(arg); n.push(this.path.join(".")); | |
71 }; | |
72 if (arg) { | |
73 if (arg.constructor == Array) { | |
74 out.push('['); | |
75 for (var i = 0; i < arg.length; ++i) { | |
76 this.path.push(i); | |
77 if (i > 0) | |
78 out.push(',\n'); | |
79 this.toJsonStringArray(arg[i], o
ut); | |
80 this.path.pop(); | |
81 } | |
82 out.push(']'); | |
83 return out; | |
84 } else if (typeof arg.toString != 'undefined') { | |
85 out.push('{'); | |
86 var first = true; | |
87 for (var i in arg) { | |
88 if(!this.includeProtos && arg[i]
===arg.constructor.prototype[i]){continue}; | |
89 this.path.push(i); | |
90 var curr = out.length; | |
91 if (!first) | |
92 out.push(this.compactOut
put?',':',\n'); | |
93 this.toJsonStringArray(i, out); | |
94 out.push(':');
| |
95 this.toJsonStringArray(arg[i], o
ut); | |
96 if (out[out.length - 1] == u) | |
97 out.splice(curr, out.len
gth - curr); | |
98 else | |
99 first = false; | |
100 this.path.pop(); | |
101 } | |
102 out.push('}'); | |
103 return out; | |
104 } | |
105 return out; | |
106 } | |
107 out.push('null'); | |
108 return out; | |
109 case 'unknown': | |
110 case 'undefined': | |
111 case 'function': | |
112 if(!this.includeFunctions){out.push(u);return out}; | |
113 arg="JSONincludedFunc:"+arg; | |
114 out.push('"'); | |
115 var a=['\\','\\\\','\n','\\n','\r','\\r','"','\\"'];arg+
=""; | |
116 for(var i=0;i<8;i+=2){arg=arg.split(a[i]).join(a[i+1])}; | |
117 out.push(arg); | |
118 out.push('"'); | |
119 return out; | |
120 case 'string': | |
121 if(this.restore && arg.indexOf("JSONcircRef:")==0){ | |
122 this.restoreCode.push('this.myObj.'+this.path.jo
in(".")+"="+arg.split("JSONcircRef:").join("this.myObj.")); | |
123 }; | |
124 out.push('"'); | |
125 var a=['\n','\\n','\r','\\r','"','\\"']; | |
126 arg+=""; for(var i=0;i<6;i+=2){arg=arg.split(a[i]).join(
a[i+1])}; | |
127 out.push(arg); | |
128 out.push('"'); | |
129 return out; | |
130 default: | |
131 out.push(String(arg)); | |
132 return out; | |
133 } | |
134 } | |
135 }; | |
OLD | NEW |