author | Dan |
Mon, 01 Feb 2010 02:15:04 -0500 | |
changeset 1216 | 4125e19d3b27 |
parent 873 | 285f0c6d9c9a |
child 1227 | bdac73ed481e |
permissions | -rw-r--r-- |
1 | 1 |
/* |
2 |
json.js |
|
3 |
2007-03-20 |
|
4 |
||
5 |
All of the code contained within this file is released into |
|
6 |
the public domain. Optionally, you may distribute this code |
|
7 |
under the terms of the GNU General Public License as well |
|
8 |
(public domain licensing allows this). |
|
9 |
||
10 |
*/ |
|
11 |
||
12 |
function toJSONString(input) |
|
13 |
{ |
|
873 | 14 |
if ( window.JSON ) |
15 |
{ |
|
16 |
return window.JSON.stringify(input); |
|
17 |
} |
|
1 | 18 |
var m = { |
19 |
'\b': '\\b', |
|
20 |
'\t': '\\t', |
|
21 |
'\n': '\\n', |
|
22 |
'\f': '\\f', |
|
23 |
'\r': '\\r', |
|
24 |
'"' : '\\"', |
|
25 |
'\\': '\\\\' |
|
26 |
}; |
|
27 |
var t = typeof(input); |
|
28 |
switch(t) |
|
29 |
{ |
|
30 |
case 'string': |
|
31 |
if (/["\\\x00-\x1f]/.test(input)) |
|
32 |
{ |
|
33 |
return '"' + input.replace(/([\x00-\x1f\\"])/g, function(a, b) |
|
34 |
{ |
|
35 |
var c = m[b]; |
|
36 |
if (c) { |
|
37 |
return c; |
|
38 |
} |
|
39 |
c = b.charCodeAt(); |
|
40 |
return '\\u00' + |
|
41 |
Math.floor(c / 16).toString(16) + |
|
42 |
(c % 16).toString(16); |
|
43 |
}) + '"'; |
|
44 |
} |
|
45 |
return '"' + input + '"'; |
|
46 |
break; |
|
47 |
case 'array': |
|
48 |
var a = ['['], |
|
49 |
b, |
|
50 |
i, |
|
51 |
l = input.length, |
|
52 |
v; |
|
53 |
||
40
723bb7acf914
Fixed a lot of bugs with Safari and Konqueror; improved Opera compatibility
Dan
parents:
1
diff
changeset
|
54 |
var p = function (s) { |
1 | 55 |
|
56 |
if (b) { |
|
57 |
a.push(','); |
|
58 |
} |
|
59 |
a.push(s); |
|
60 |
b = true; |
|
61 |
} |
|
62 |
||
63 |
for (i = 0; i < l; i += 1) { |
|
64 |
v = input[i]; |
|
65 |
switch (typeof v) { |
|
66 |
case 'object': |
|
67 |
if (v) { |
|
68 |
p(toJSONString(v)); |
|
69 |
} else { |
|
70 |
p("null"); |
|
71 |
} |
|
72 |
break; |
|
73 |
case 'array': |
|
74 |
case 'string': |
|
75 |
case 'number': |
|
76 |
case 'boolean': |
|
77 |
p(toJSONString(v)); |
|
78 |
} |
|
79 |
} |
|
80 |
||
81 |
a.push(']'); |
|
82 |
return a.join(''); |
|
83 |
break; |
|
84 |
case 'date': |
|
40
723bb7acf914
Fixed a lot of bugs with Safari and Konqueror; improved Opera compatibility
Dan
parents:
1
diff
changeset
|
85 |
var f = function (n) |
1 | 86 |
{ |
87 |
return n < 10 ? '0' + n : n; |
|
88 |
} |
|
89 |
return '"' + input.getFullYear() + '-' + |
|
90 |
f(input.getMonth() + 1) + '-' + |
|
91 |
f(input.getDate()) + 'T' + |
|
92 |
f(input.getHours()) + ':' + |
|
93 |
f(input.getMinutes()) + ':' + |
|
94 |
f(input.getSeconds()) + '"'; |
|
95 |
||
96 |
case 'boolean': |
|
97 |
return String(input); |
|
98 |
break; |
|
99 |
case 'number': |
|
100 |
return isFinite(input) ? String(input) : "null"; |
|
101 |
break; |
|
102 |
case 'object': |
|
103 |
var a = ['{'], |
|
104 |
b, |
|
105 |
k, |
|
106 |
v; |
|
107 |
||
40
723bb7acf914
Fixed a lot of bugs with Safari and Konqueror; improved Opera compatibility
Dan
parents:
1
diff
changeset
|
108 |
var p = function (s) |
1 | 109 |
{ |
110 |
if (b) |
|
111 |
{ |
|
112 |
a.push(','); |
|
113 |
} |
|
114 |
a.push(toJSONString(k), ':', s); |
|
115 |
b = true; |
|
116 |
} |
|
117 |
||
118 |
for (k in input) |
|
119 |
{ |
|
120 |
if (input.hasOwnProperty(k)) |
|
121 |
{ |
|
122 |
v = input[k]; |
|
123 |
switch (typeof v) { |
|
124 |
||
125 |
case 'object': |
|
126 |
if (v) { |
|
127 |
p(toJSONString(v)); |
|
128 |
} else { |
|
129 |
p("null"); |
|
130 |
} |
|
131 |
break; |
|
132 |
case 'string': |
|
133 |
case 'number': |
|
134 |
case 'boolean': |
|
135 |
p(toJSONString(v)); |
|
136 |
break; |
|
137 |
} |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
a.push('}'); |
|
142 |
return a.join(''); |
|
143 |
break; |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
function parseJSON(string, filter) |
|
148 |
{ |
|
873 | 149 |
if ( window.JSON ) |
150 |
{ |
|
151 |
return window.JSON.parse(string); |
|
152 |
} |
|
153 |
||
1 | 154 |
try { |
155 |
if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/. |
|
156 |
test(string)) |
|
157 |
{ |
|
158 |
||
159 |
var j = eval('(' + string + ')'); |
|
160 |
if (typeof filter === 'function') { |
|
161 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
40
diff
changeset
|
162 |
function walk(k, v) |
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
40
diff
changeset
|
163 |
{ |
1 | 164 |
if (v && typeof v === 'object') { |
165 |
for (var i in v) { |
|
166 |
if (v.hasOwnProperty(i)) { |
|
167 |
v[i] = walk(i, v[i]); |
|
168 |
} |
|
169 |
} |
|
170 |
} |
|
171 |
return filter(k, v); |
|
172 |
} |
|
173 |
||
174 |
j = walk('', j); |
|
175 |
} |
|
176 |
return j; |
|
177 |
} |
|
178 |
} catch (e) { |
|
179 |
||
180 |
} |
|
181 |
throw new SyntaxError("parseJSON"); |
|
182 |
} |