1 /* |
1 /* |
2 http://www.JSON.org/json2.js |
2 http://www.JSON.org/json2.js |
3 2009-08-17 |
3 2011-02-23 |
4 |
4 |
5 Public Domain. |
5 Public Domain. |
6 |
6 |
7 NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. |
7 NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. |
8 |
8 |
9 See http://www.JSON.org/js.html |
9 See http://www.JSON.org/js.html |
|
10 |
|
11 |
|
12 This code should be minified before deployment. |
|
13 See http://javascript.crockford.com/jsmin.html |
|
14 |
|
15 USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO |
|
16 NOT CONTROL. |
|
17 |
10 |
18 |
11 This file creates a global JSON object containing two methods: stringify |
19 This file creates a global JSON object containing two methods: stringify |
12 and parse. |
20 and parse. |
13 |
21 |
14 JSON.stringify(value, replacer, space) |
22 JSON.stringify(value, replacer, space) |
134 }); |
142 }); |
135 |
143 |
136 |
144 |
137 This is a reference implementation. You are free to copy, modify, or |
145 This is a reference implementation. You are free to copy, modify, or |
138 redistribute. |
146 redistribute. |
139 |
|
140 This code should be minified before deployment. |
|
141 See http://javascript.crockford.com/jsmin.html |
|
142 |
|
143 USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO |
|
144 NOT CONTROL. |
|
145 */ |
147 */ |
146 |
148 |
147 /*jslint evil: true */ |
149 /*jslint evil: true, strict: false, regexp: false */ |
148 |
150 |
149 /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply, |
151 /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply, |
150 call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours, |
152 call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours, |
151 getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join, |
153 getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join, |
152 lastIndex, length, parse, prototype, push, replace, slice, stringify, |
154 lastIndex, length, parse, prototype, push, replace, slice, stringify, |
153 test, toJSON, toString, valueOf |
155 test, toJSON, toString, valueOf |
154 */ |
156 */ |
155 |
157 |
156 "use strict"; |
|
157 |
158 |
158 // Create a JSON object only if one does not already exist. We create the |
159 // Create a JSON object only if one does not already exist. We create the |
159 // methods in a closure to avoid creating global variables. |
160 // methods in a closure to avoid creating global variables. |
160 |
161 |
161 if (!this.JSON) { |
162 var JSON; |
162 this.JSON = {}; |
163 if (!JSON) { |
|
164 JSON = {}; |
163 } |
165 } |
164 |
166 |
165 (function () { |
167 (function () { |
|
168 "use strict"; |
166 |
169 |
167 function f(n) { |
170 function f(n) { |
168 // Format integers to have at least two digits. |
171 // Format integers to have at least two digits. |
169 return n < 10 ? '0' + n : n; |
172 return n < 10 ? '0' + n : n; |
170 } |
173 } |
172 if (typeof Date.prototype.toJSON !== 'function') { |
175 if (typeof Date.prototype.toJSON !== 'function') { |
173 |
176 |
174 Date.prototype.toJSON = function (key) { |
177 Date.prototype.toJSON = function (key) { |
175 |
178 |
176 return isFinite(this.valueOf()) ? |
179 return isFinite(this.valueOf()) ? |
177 this.getUTCFullYear() + '-' + |
180 this.getUTCFullYear() + '-' + |
178 f(this.getUTCMonth() + 1) + '-' + |
181 f(this.getUTCMonth() + 1) + '-' + |
179 f(this.getUTCDate()) + 'T' + |
182 f(this.getUTCDate()) + 'T' + |
180 f(this.getUTCHours()) + ':' + |
183 f(this.getUTCHours()) + ':' + |
181 f(this.getUTCMinutes()) + ':' + |
184 f(this.getUTCMinutes()) + ':' + |
182 f(this.getUTCSeconds()) + 'Z' : null; |
185 f(this.getUTCSeconds()) + 'Z' : null; |
183 }; |
186 }; |
184 |
187 |
185 String.prototype.toJSON = |
188 String.prototype.toJSON = |
186 Number.prototype.toJSON = |
189 Number.prototype.toJSON = |
187 Boolean.prototype.toJSON = function (key) { |
190 Boolean.prototype.toJSON = function (key) { |
188 return this.valueOf(); |
191 return this.valueOf(); |
189 }; |
192 }; |
190 } |
193 } |
191 |
194 |
192 var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, |
195 var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, |
193 escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, |
196 escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, |
194 gap, |
197 gap, |
211 // backslash characters, then we can safely slap some quotes around it. |
214 // backslash characters, then we can safely slap some quotes around it. |
212 // Otherwise we must also replace the offending characters with safe escape |
215 // Otherwise we must also replace the offending characters with safe escape |
213 // sequences. |
216 // sequences. |
214 |
217 |
215 escapable.lastIndex = 0; |
218 escapable.lastIndex = 0; |
216 return escapable.test(string) ? |
219 return escapable.test(string) ? '"' + string.replace(escapable, function (a) { |
217 '"' + string.replace(escapable, function (a) { |
220 var c = meta[a]; |
218 var c = meta[a]; |
221 return typeof c === 'string' ? c : |
219 return typeof c === 'string' ? c : |
222 '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); |
220 '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); |
223 }) + '"' : '"' + string + '"'; |
221 }) + '"' : |
|
222 '"' + string + '"'; |
|
223 } |
224 } |
224 |
225 |
225 |
226 |
226 function str(key, holder) { |
227 function str(key, holder) { |
227 |
228 |
300 } |
301 } |
301 |
302 |
302 // Join all of the elements together, separated with commas, and wrap them in |
303 // Join all of the elements together, separated with commas, and wrap them in |
303 // brackets. |
304 // brackets. |
304 |
305 |
305 v = partial.length === 0 ? '[]' : |
306 v = partial.length === 0 ? '[]' : gap ? |
306 gap ? '[\n' + gap + |
307 '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' : |
307 partial.join(',\n' + gap) + '\n' + |
308 '[' + partial.join(',') + ']'; |
308 mind + ']' : |
|
309 '[' + partial.join(',') + ']'; |
|
310 gap = mind; |
309 gap = mind; |
311 return v; |
310 return v; |
312 } |
311 } |
313 |
312 |
314 // If the replacer is an array, use it to select the members to be stringified. |
313 // If the replacer is an array, use it to select the members to be stringified. |
315 |
314 |
316 if (rep && typeof rep === 'object') { |
315 if (rep && typeof rep === 'object') { |
317 length = rep.length; |
316 length = rep.length; |
318 for (i = 0; i < length; i += 1) { |
317 for (i = 0; i < length; i += 1) { |
319 k = rep[i]; |
318 if (typeof rep[i] === 'string') { |
320 if (typeof k === 'string') { |
319 k = rep[i]; |
321 v = str(k, value); |
320 v = str(k, value); |
322 if (v) { |
321 if (v) { |
323 partial.push(quote(k) + (gap ? ': ' : ':') + v); |
322 partial.push(quote(k) + (gap ? ': ' : ':') + v); |
324 } |
323 } |
325 } |
324 } |
339 } |
338 } |
340 |
339 |
341 // Join all of the member texts together, separated with commas, |
340 // Join all of the member texts together, separated with commas, |
342 // and wrap them in braces. |
341 // and wrap them in braces. |
343 |
342 |
344 v = partial.length === 0 ? '{}' : |
343 v = partial.length === 0 ? '{}' : gap ? |
345 gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + |
344 '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' : |
346 mind + '}' : '{' + partial.join(',') + '}'; |
345 '{' + partial.join(',') + '}'; |
347 gap = mind; |
346 gap = mind; |
348 return v; |
347 return v; |
349 } |
348 } |
350 } |
349 } |
351 |
350 |
382 // Otherwise, throw an error. |
381 // Otherwise, throw an error. |
383 |
382 |
384 rep = replacer; |
383 rep = replacer; |
385 if (replacer && typeof replacer !== 'function' && |
384 if (replacer && typeof replacer !== 'function' && |
386 (typeof replacer !== 'object' || |
385 (typeof replacer !== 'object' || |
387 typeof replacer.length !== 'number')) { |
386 typeof replacer.length !== 'number')) { |
388 throw new Error('JSON.stringify'); |
387 throw new Error('JSON.stringify'); |
389 } |
388 } |
390 |
389 |
391 // Make a fake root object containing our value under the key of ''. |
390 // Make a fake root object containing our value under the key of ''. |
392 // Return the result of stringifying the value. |
391 // Return the result of stringifying the value. |
412 // that modifications can be made. |
411 // that modifications can be made. |
413 |
412 |
414 var k, v, value = holder[key]; |
413 var k, v, value = holder[key]; |
415 if (value && typeof value === 'object') { |
414 if (value && typeof value === 'object') { |
416 for (k in value) { |
415 for (k in value) { |
417 if (Object.hasOwnProperty.call(value, k)) { |
416 if (Object.prototype.hasOwnProperty.call(value, k)) { |
418 v = walk(value, k); |
417 v = walk(value, k); |
419 if (v !== undefined) { |
418 if (v !== undefined) { |
420 value[k] = v; |
419 value[k] = v; |
421 } else { |
420 } else { |
422 delete value[k]; |
421 delete value[k]; |
430 |
429 |
431 // Parsing happens in four stages. In the first stage, we replace certain |
430 // Parsing happens in four stages. In the first stage, we replace certain |
432 // Unicode characters with escape sequences. JavaScript handles many characters |
431 // Unicode characters with escape sequences. JavaScript handles many characters |
433 // incorrectly, either silently deleting them, or treating them as line endings. |
432 // incorrectly, either silently deleting them, or treating them as line endings. |
434 |
433 |
|
434 text = String(text); |
435 cx.lastIndex = 0; |
435 cx.lastIndex = 0; |
436 if (cx.test(text)) { |
436 if (cx.test(text)) { |
437 text = text.replace(cx, function (a) { |
437 text = text.replace(cx, function (a) { |
438 return '\\u' + |
438 return '\\u' + |
439 ('0000' + a.charCodeAt(0).toString(16)).slice(-4); |
439 ('0000' + a.charCodeAt(0).toString(16)).slice(-4); |
451 // replace all simple value tokens with ']' characters. Third, we delete all |
451 // replace all simple value tokens with ']' characters. Third, we delete all |
452 // open brackets that follow a colon or comma or that begin the text. Finally, |
452 // open brackets that follow a colon or comma or that begin the text. Finally, |
453 // we look to see that the remaining characters are only whitespace or ']' or |
453 // we look to see that the remaining characters are only whitespace or ']' or |
454 // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval. |
454 // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval. |
455 |
455 |
456 if (/^[\],:{}\s]*$/. |
456 if (/^[\],:{}\s]*$/ |
457 test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@'). |
457 .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') |
458 replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'). |
458 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') |
459 replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { |
459 .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { |
460 |
460 |
461 // In the third stage we use the eval function to compile the text into a |
461 // In the third stage we use the eval function to compile the text into a |
462 // JavaScript structure. The '{' operator is subject to a syntactic ambiguity |
462 // JavaScript structure. The '{' operator is subject to a syntactic ambiguity |
463 // in JavaScript: it can begin a block or an object literal. We wrap the text |
463 // in JavaScript: it can begin a block or an object literal. We wrap the text |
464 // in parens to eliminate the ambiguity. |
464 // in parens to eliminate the ambiguity. |