author | Dan |
Fri, 07 Sep 2007 16:39:42 -0400 | |
changeset 121 | 1eb7b2b4bdbc |
parent 40 | 723bb7acf914 |
child 582 | a38876c0793c |
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 |
{ |
|
14 |
var m = { |
|
15 |
'\b': '\\b', |
|
16 |
'\t': '\\t', |
|
17 |
'\n': '\\n', |
|
18 |
'\f': '\\f', |
|
19 |
'\r': '\\r', |
|
20 |
'"' : '\\"', |
|
21 |
'\\': '\\\\' |
|
22 |
}; |
|
23 |
var t = typeof(input); |
|
24 |
switch(t) |
|
25 |
{ |
|
26 |
case 'string': |
|
27 |
if (/["\\\x00-\x1f]/.test(input)) |
|
28 |
{ |
|
29 |
return '"' + input.replace(/([\x00-\x1f\\"])/g, function(a, b) |
|
30 |
{ |
|
31 |
var c = m[b]; |
|
32 |
if (c) { |
|
33 |
return c; |
|
34 |
} |
|
35 |
c = b.charCodeAt(); |
|
36 |
return '\\u00' + |
|
37 |
Math.floor(c / 16).toString(16) + |
|
38 |
(c % 16).toString(16); |
|
39 |
}) + '"'; |
|
40 |
} |
|
41 |
return '"' + input + '"'; |
|
42 |
break; |
|
43 |
case 'array': |
|
44 |
var a = ['['], |
|
45 |
b, |
|
46 |
i, |
|
47 |
l = input.length, |
|
48 |
v; |
|
49 |
||
40
723bb7acf914
Fixed a lot of bugs with Safari and Konqueror; improved Opera compatibility
Dan
parents:
1
diff
changeset
|
50 |
var p = function (s) { |
1 | 51 |
|
52 |
if (b) { |
|
53 |
a.push(','); |
|
54 |
} |
|
55 |
a.push(s); |
|
56 |
b = true; |
|
57 |
} |
|
58 |
||
59 |
for (i = 0; i < l; i += 1) { |
|
60 |
v = input[i]; |
|
61 |
switch (typeof v) { |
|
62 |
case 'object': |
|
63 |
if (v) { |
|
64 |
p(toJSONString(v)); |
|
65 |
} else { |
|
66 |
p("null"); |
|
67 |
} |
|
68 |
break; |
|
69 |
case 'array': |
|
70 |
case 'string': |
|
71 |
case 'number': |
|
72 |
case 'boolean': |
|
73 |
p(toJSONString(v)); |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
a.push(']'); |
|
78 |
return a.join(''); |
|
79 |
break; |
|
80 |
case 'date': |
|
40
723bb7acf914
Fixed a lot of bugs with Safari and Konqueror; improved Opera compatibility
Dan
parents:
1
diff
changeset
|
81 |
var f = function (n) |
1 | 82 |
{ |
83 |
return n < 10 ? '0' + n : n; |
|
84 |
} |
|
85 |
return '"' + input.getFullYear() + '-' + |
|
86 |
f(input.getMonth() + 1) + '-' + |
|
87 |
f(input.getDate()) + 'T' + |
|
88 |
f(input.getHours()) + ':' + |
|
89 |
f(input.getMinutes()) + ':' + |
|
90 |
f(input.getSeconds()) + '"'; |
|
91 |
||
92 |
case 'boolean': |
|
93 |
return String(input); |
|
94 |
break; |
|
95 |
case 'number': |
|
96 |
return isFinite(input) ? String(input) : "null"; |
|
97 |
break; |
|
98 |
case 'object': |
|
99 |
var a = ['{'], |
|
100 |
b, |
|
101 |
k, |
|
102 |
v; |
|
103 |
||
40
723bb7acf914
Fixed a lot of bugs with Safari and Konqueror; improved Opera compatibility
Dan
parents:
1
diff
changeset
|
104 |
var p = function (s) |
1 | 105 |
{ |
106 |
if (b) |
|
107 |
{ |
|
108 |
a.push(','); |
|
109 |
} |
|
110 |
a.push(toJSONString(k), ':', s); |
|
111 |
b = true; |
|
112 |
} |
|
113 |
||
114 |
for (k in input) |
|
115 |
{ |
|
116 |
if (input.hasOwnProperty(k)) |
|
117 |
{ |
|
118 |
v = input[k]; |
|
119 |
switch (typeof v) { |
|
120 |
||
121 |
case 'object': |
|
122 |
if (v) { |
|
123 |
p(toJSONString(v)); |
|
124 |
} else { |
|
125 |
p("null"); |
|
126 |
} |
|
127 |
break; |
|
128 |
case 'string': |
|
129 |
case 'number': |
|
130 |
case 'boolean': |
|
131 |
p(toJSONString(v)); |
|
132 |
break; |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
||
137 |
a.push('}'); |
|
138 |
return a.join(''); |
|
139 |
break; |
|
140 |
} |
|
141 |
} |
|
142 |
||
143 |
function parseJSON(string, filter) |
|
144 |
{ |
|
145 |
try { |
|
146 |
if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/. |
|
147 |
test(string)) |
|
148 |
{ |
|
149 |
||
150 |
var j = eval('(' + string + ')'); |
|
151 |
if (typeof filter === 'function') { |
|
152 |
||
153 |
function walk(k, v) { |
|
154 |
if (v && typeof v === 'object') { |
|
155 |
for (var i in v) { |
|
156 |
if (v.hasOwnProperty(i)) { |
|
157 |
v[i] = walk(i, v[i]); |
|
158 |
} |
|
159 |
} |
|
160 |
} |
|
161 |
return filter(k, v); |
|
162 |
} |
|
163 |
||
164 |
j = walk('', j); |
|
165 |
} |
|
166 |
return j; |
|
167 |
} |
|
168 |
} catch (e) { |
|
169 |
||
170 |
} |
|
171 |
throw new SyntaxError("parseJSON"); |
|
172 |
} |