7 NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
9 See http://www.JSON.org/js.html
12 This code should be minified before deployment.
13 See http://javascript.crockford.com/jsmin.html
15 USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
19 This file creates a global JSON object containing two methods: stringify
20 and parse. This file is provides the ES5 JSON capability to ES3 systems.
21 If a project might run on IE8 or earlier, then this file should be included.
22 This file does nothing on ES5 systems.
24 JSON.stringify(value, replacer, space)
25 value any JavaScript value, usually an object or array.
27 replacer an optional parameter that determines how object
28 values are stringified for objects. It can be a
29 function or an array of strings.
31 space an optional parameter that specifies the indentation
32 of nested structures. If it is omitted, the text will
33 be packed without extra whitespace. If it is a number,
34 it will specify the number of spaces to indent at each
35 level. If it is a string (such as '\t' or ' '),
36 it contains the characters used to indent at each level.
38 This method produces a JSON text from a JavaScript value.
40 When an object value is found, if the object contains a toJSON
41 method, its toJSON method will be called and the result will be
42 stringified. A toJSON method does not serialize: it returns the
43 value represented by the name/value pair that should be serialized,
44 or undefined if nothing should be serialized. The toJSON method
45 will be passed the key associated with the value, and this will be
48 For example, this would serialize Dates as ISO strings.
50 Date.prototype.toJSON = function (key) {
52 // Format integers to have at least two digits.
58 return this.getUTCFullYear() + '-' +
59 f(this.getUTCMonth() + 1) + '-' +
60 f(this.getUTCDate()) + 'T' +
61 f(this.getUTCHours()) + ':' +
62 f(this.getUTCMinutes()) + ':' +
63 f(this.getUTCSeconds()) + 'Z';
66 You can provide an optional replacer method. It will be passed the
67 key and value of each member, with this bound to the containing
68 object. The value that is returned from your method will be
69 serialized. If your method returns undefined, then the member will
70 be excluded from the serialization.
72 If the replacer parameter is an array of strings, then it will be
73 used to select the members to be serialized. It filters the results
74 such that only members with keys listed in the replacer array are
77 Values that do not have JSON representations, such as undefined or
78 functions, will not be serialized. Such values in objects will be
79 dropped; in arrays they will be replaced with null. You can use
80 a replacer function to replace those with JSON values.
81 JSON.stringify(undefined) returns undefined.
83 The optional space parameter produces a stringification of the
84 value that is filled with line breaks and indentation to make it
87 If the space parameter is a non-empty string, then that string will
88 be used for indentation. If the space parameter is a number, then
89 the indentation will be that many spaces.
93 text = JSON.stringify(['e', {pluribus: 'unum'}]);
94 // text is '["e",{"pluribus":"unum"}]'
97 text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
98 // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
100 text = JSON.stringify([new Date()], function (key, value) {
101 return this[key] instanceof Date
102 ? 'Date(' + this[key] + ')'
105 // text is '["Date(---current time---)"]'
108 JSON.parse(text, reviver)
109 This method parses a JSON text to produce an object or array.
110 It can throw a SyntaxError exception.
112 The optional reviver parameter is a function that can filter and
113 transform the results. It receives each of the keys and values,
114 and its return value is used instead of the original value.
115 If it returns what it received, then the structure is not modified.
116 If it returns undefined then the member is deleted.
120 // Parse the text. Values that look like ISO date strings will
121 // be converted to Date objects.
123 myData = JSON.parse(text, function (key, value) {
125 if (typeof value === 'string') {
127 /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
129 return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
136 myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
138 if (typeof value === 'string' &&
139 value.slice(0, 5) === 'Date(' &&
140 value.slice(-1) === ')') {
141 d = new Date(value.slice(5, -1));
150 This is a reference implementation. You are free to copy, modify, or
159 JSON, apply, call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
160 getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
161 lastIndex, length, parse, prototype, push, replace, slice, stringify,
162 test, toJSON, toString, valueOf
166 // Create a JSON object only if one does not already exist. We create the
167 // methods in a closure to avoid creating global variables.
169 if (typeof JSON !== 'object') {
176 var rx_one = /^[\],:{}\s]*$/,
177 rx_two = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
178 rx_three = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
179 rx_four = /(?:^|:|,)(?:\s*\[)+/g,
180 rx_escapable = /[\\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
181 rx_dangerous = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
184 // Format integers to have at least two digits.
190 function this_value() {
191 return this.valueOf();
194 if (typeof Date.prototype.toJSON !== 'function') {
196 Date.prototype.toJSON = function () {
198 return isFinite(this.valueOf())
199 ? this.getUTCFullYear() + '-' +
200 f(this.getUTCMonth() + 1) + '-' +
201 f(this.getUTCDate()) + 'T' +
202 f(this.getUTCHours()) + ':' +
203 f(this.getUTCMinutes()) + ':' +
204 f(this.getUTCSeconds()) + 'Z'
208 Boolean.prototype.toJSON = this_value;
209 Number.prototype.toJSON = this_value;
210 String.prototype.toJSON = this_value;
219 function quote(string) {
221 // If the string contains no control characters, no quote characters, and no
222 // backslash characters, then we can safely slap some quotes around it.
223 // Otherwise we must also replace the offending characters with safe escape
226 rx_escapable.lastIndex = 0;
227 return rx_escapable.test(string)
228 ? '"' + string.replace(rx_escapable, function (a) {
230 return typeof c === 'string'
232 : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
234 : '"' + string + '"';
238 function str(key, holder) {
240 // Produce a string from holder[key].
242 var i, // The loop counter.
243 k, // The member key.
244 v, // The member value.
250 // If the value has a toJSON method, call it to obtain a replacement value.
252 if (value && typeof value === 'object' &&
253 typeof value.toJSON === 'function') {
254 value = value.toJSON(key);
257 // If we were called with a replacer function, then call the replacer to
258 // obtain a replacement value.
260 if (typeof rep === 'function') {
261 value = rep.call(holder, key, value);
264 // What happens next depends on the value's type.
266 switch (typeof value) {
272 // JSON numbers must be finite. Encode non-finite numbers as null.
274 return isFinite(value)
281 // If the value is a boolean or null, convert it to a string. Note:
282 // typeof null does not produce 'null'. The case is included here in
283 // the remote chance that this gets fixed someday.
285 return String(value);
287 // If the type is 'object', we might be dealing with an object or an array or
292 // Due to a specification blunder in ECMAScript, typeof null is 'object',
293 // so watch out for that case.
299 // Make an array to hold the partial results of stringifying this object value.
304 // Is the value an array?
306 if (Object.prototype.toString.apply(value) === '[object Array]') {
308 // The value is an array. Stringify every element. Use null as a placeholder
309 // for non-JSON values.
311 length = value.length;
312 for (i = 0; i < length; i += 1) {
313 partial[i] = str(i, value) || 'null';
316 // Join all of the elements together, separated with commas, and wrap them in
319 v = partial.length === 0
322 ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
323 : '[' + partial.join(',') + ']';
328 // If the replacer is an array, use it to select the members to be stringified.
330 if (rep && typeof rep === 'object') {
332 for (i = 0; i < length; i += 1) {
333 if (typeof rep[i] === 'string') {
337 partial.push(quote(k) + (
347 // Otherwise, iterate through all of the keys in the object.
350 if (Object.prototype.hasOwnProperty.call(value, k)) {
353 partial.push(quote(k) + (
363 // Join all of the member texts together, separated with commas,
364 // and wrap them in braces.
366 v = partial.length === 0
369 ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
370 : '{' + partial.join(',') + '}';
376 // If the JSON object does not yet have a stringify method, give it one.
378 if (typeof JSON.stringify !== 'function') {
379 meta = { // table of character substitutions
388 JSON.stringify = function (value, replacer, space) {
390 // The stringify method takes a value and an optional replacer, and an optional
391 // space parameter, and returns a JSON text. The replacer can be a function
392 // that can replace values, or an array of strings that will select the keys.
393 // A default replacer method can be provided. Use of the space parameter can
394 // produce text that is more easily readable.
400 // If the space parameter is a number, make an indent string containing that
403 if (typeof space === 'number') {
404 for (i = 0; i < space; i += 1) {
408 // If the space parameter is a string, it will be used as the indent string.
410 } else if (typeof space === 'string') {
414 // If there is a replacer, it must be a function or an array.
415 // Otherwise, throw an error.
418 if (replacer && typeof replacer !== 'function' &&
419 (typeof replacer !== 'object' ||
420 typeof replacer.length !== 'number')) {
421 throw new Error('JSON.stringify');
424 // Make a fake root object containing our value under the key of ''.
425 // Return the result of stringifying the value.
427 return str('', {'': value});
432 // If the JSON object does not yet have a parse method, give it one.
434 if (typeof JSON.parse !== 'function') {
435 JSON.parse = function (text, reviver) {
437 // The parse method takes a text and an optional reviver function, and returns
438 // a JavaScript value if the text is a valid JSON text.
442 function walk(holder, key) {
444 // The walk method is used to recursively walk the resulting structure so
445 // that modifications can be made.
447 var k, v, value = holder[key];
448 if (value && typeof value === 'object') {
450 if (Object.prototype.hasOwnProperty.call(value, k)) {
452 if (v !== undefined) {
460 return reviver.call(holder, key, value);
464 // Parsing happens in four stages. In the first stage, we replace certain
465 // Unicode characters with escape sequences. JavaScript handles many characters
466 // incorrectly, either silently deleting them, or treating them as line endings.
469 rx_dangerous.lastIndex = 0;
470 if (rx_dangerous.test(text)) {
471 text = text.replace(rx_dangerous, function (a) {
473 ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
477 // In the second stage, we run the text against regular expressions that look
478 // for non-JSON patterns. We are especially concerned with '()' and 'new'
479 // because they can cause invocation, and '=' because it can cause mutation.
480 // But just to be safe, we want to reject all unexpected forms.
482 // We split the second stage into 4 regexp operations in order to work around
483 // crippling inefficiencies in IE's and Safari's regexp engines. First we
484 // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
485 // replace all simple value tokens with ']' characters. Third, we delete all
486 // open brackets that follow a colon or comma or that begin the text. Finally,
487 // we look to see that the remaining characters are only whitespace or ']' or
488 // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
493 .replace(rx_two, '@')
494 .replace(rx_three, ']')
495 .replace(rx_four, '')
499 // In the third stage we use the eval function to compile the text into a
500 // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
501 // in JavaScript: it can begin a block or an object literal. We wrap the text
502 // in parens to eliminate the ambiguity.
504 j = eval('(' + text + ')');
506 // In the optional fourth stage, we recursively walk the new structure, passing
507 // each name/value pair to a reviver function for possible transformation.
509 return typeof reviver === 'function'
514 // If the text is not JSON parseable, then a SyntaxError is thrown.
516 throw new SyntaxError('JSON.parse');