author | Dan |
Tue, 16 Sep 2008 08:22:47 -0400 | |
changeset 710 | 1fbce408813c |
parent 582 | a38876c0793c |
child 873 | 285f0c6d9c9a |
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 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
40
diff
changeset
|
153 |
function walk(k, v) |
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
40
diff
changeset
|
154 |
{ |
1 | 155 |
if (v && typeof v === 'object') { |
156 |
for (var i in v) { |
|
157 |
if (v.hasOwnProperty(i)) { |
|
158 |
v[i] = walk(i, v[i]); |
|
159 |
} |
|
160 |
} |
|
161 |
} |
|
162 |
return filter(k, v); |
|
163 |
} |
|
164 |
||
165 |
j = walk('', j); |
|
166 |
} |
|
167 |
return j; |
|
168 |
} |
|
169 |
} catch (e) { |
|
170 |
||
171 |
} |
|
172 |
throw new SyntaxError("parseJSON"); |
|
173 |
} |