rlm@45: rlm@45: /* rlm@45: http://www.JSON.org/json2.js rlm@45: 2009-09-29 rlm@45: rlm@45: Public Domain. rlm@45: rlm@45: NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. rlm@45: rlm@45: See http://www.JSON.org/js.html rlm@45: rlm@45: rlm@45: This code should be minified before deployment. rlm@45: See http://javascript.crockford.com/jsmin.html rlm@45: rlm@45: USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO rlm@45: NOT CONTROL. rlm@45: rlm@45: rlm@45: This file creates a global JSON object containing two methods: stringify rlm@45: and parse. rlm@45: rlm@45: JSON.stringify(value, replacer, space) rlm@45: value any JavaScript value, usually an object or array. rlm@45: rlm@45: replacer an optional parameter that determines how object rlm@45: values are stringified for objects. It can be a rlm@45: function or an array of strings. rlm@45: rlm@45: space an optional parameter that specifies the indentation rlm@45: of nested structures. If it is omitted, the text will rlm@45: be packed without extra whitespace. If it is a number, rlm@45: it will specify the number of spaces to indent at each rlm@45: level. If it is a string (such as '\t' or ' '), rlm@45: it contains the characters used to indent at each level. rlm@45: rlm@45: This method produces a JSON text from a JavaScript value. rlm@45: rlm@45: When an object value is found, if the object contains a toJSON rlm@45: method, its toJSON method will be called and the result will be rlm@45: stringified. A toJSON method does not serialize: it returns the rlm@45: value represented by the name/value pair that should be serialized, rlm@45: or undefined if nothing should be serialized. The toJSON method rlm@45: will be passed the key associated with the value, and this will be rlm@45: bound to the value rlm@45: rlm@45: For example, this would serialize Dates as ISO strings. rlm@45: rlm@45: Date.prototype.toJSON = function (key) { rlm@45: function f(n) { rlm@45: // Format integers to have at least two digits. rlm@45: return n < 10 ? '0' + n : n; rlm@45: } rlm@45: rlm@45: return this.getUTCFullYear() + '-' + rlm@45: f(this.getUTCMonth() + 1) + '-' + rlm@45: f(this.getUTCDate()) + 'T' + rlm@45: f(this.getUTCHours()) + ':' + rlm@45: f(this.getUTCMinutes()) + ':' + rlm@45: f(this.getUTCSeconds()) + 'Z'; rlm@45: }; rlm@45: rlm@45: You can provide an optional replacer method. It will be passed the rlm@45: key and value of each member, with this bound to the containing rlm@45: object. The value that is returned from your method will be rlm@45: serialized. If your method returns undefined, then the member will rlm@45: be excluded from the serialization. rlm@45: rlm@45: If the replacer parameter is an array of strings, then it will be rlm@45: used to select the members to be serialized. It filters the results rlm@45: such that only members with keys listed in the replacer array are rlm@45: stringified. rlm@45: rlm@45: Values that do not have JSON representations, such as undefined or rlm@45: functions, will not be serialized. Such values in objects will be rlm@45: dropped; in arrays they will be replaced with null. You can use rlm@45: a replacer function to replace those with JSON values. rlm@45: JSON.stringify(undefined) returns undefined. rlm@45: rlm@45: The optional space parameter produces a stringification of the rlm@45: value that is filled with line breaks and indentation to make it rlm@45: easier to read. rlm@45: rlm@45: If the space parameter is a non-empty string, then that string will rlm@45: be used for indentation. If the space parameter is a number, then rlm@45: the indentation will be that many spaces. rlm@45: rlm@45: Example: rlm@45: rlm@45: text = JSON.stringify(['e', {pluribus: 'unum'}]); rlm@45: // text is '["e",{"pluribus":"unum"}]' rlm@45: rlm@45: rlm@45: text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t'); rlm@45: // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]' rlm@45: rlm@45: text = JSON.stringify([new Date()], function (key, value) { rlm@45: return this[key] instanceof Date ? rlm@45: 'Date(' + this[key] + ')' : value; rlm@45: }); rlm@45: // text is '["Date(---current time---)"]' rlm@45: rlm@45: rlm@45: JSON.parse(text, reviver) rlm@45: This method parses a JSON text to produce an object or array. rlm@45: It can throw a SyntaxError exception. rlm@45: rlm@45: The optional reviver parameter is a function that can filter and rlm@45: transform the results. It receives each of the keys and values, rlm@45: and its return value is used instead of the original value. rlm@45: If it returns what it received, then the structure is not modified. rlm@45: If it returns undefined then the member is deleted. rlm@45: rlm@45: Example: rlm@45: rlm@45: // Parse the text. Values that look like ISO date strings will rlm@45: // be converted to Date objects. rlm@45: rlm@45: myData = JSON.parse(text, function (key, value) { rlm@45: var a; rlm@45: if (typeof value === 'string') { rlm@45: a = rlm@45: /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value); rlm@45: if (a) { rlm@45: return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], rlm@45: +a[5], +a[6])); rlm@45: } rlm@45: } rlm@45: return value; rlm@45: }); rlm@45: rlm@45: myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) { rlm@45: var d; rlm@45: if (typeof value === 'string' && rlm@45: value.slice(0, 5) === 'Date(' && rlm@45: value.slice(-1) === ')') { rlm@45: d = new Date(value.slice(5, -1)); rlm@45: if (d) { rlm@45: return d; rlm@45: } rlm@45: } rlm@45: return value; rlm@45: }); rlm@45: rlm@45: rlm@45: This is a reference implementation. You are free to copy, modify, or rlm@45: redistribute. rlm@45: */ rlm@45: rlm@45: /*jslint evil: true, strict: false */ rlm@45: rlm@45: /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply, rlm@45: call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours, rlm@45: getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join, rlm@45: lastIndex, length, parse, prototype, push, replace, slice, stringify, rlm@45: test, toJSON, toString, valueOf rlm@45: */ rlm@45: rlm@45: rlm@45: // Create a JSON object only if one does not already exist. We create the rlm@45: // methods in a closure to avoid creating global variables. rlm@45: rlm@45: if (!this.JSON) { rlm@45: this.JSON = {}; rlm@45: } rlm@45: rlm@45: (function () { rlm@45: rlm@45: function f(n) { rlm@45: // Format integers to have at least two digits. rlm@45: return n < 10 ? '0' + n : n; rlm@45: } rlm@45: rlm@45: if (typeof Date.prototype.toJSON !== 'function') { rlm@45: rlm@45: Date.prototype.toJSON = function (key) { rlm@45: rlm@45: return isFinite(this.valueOf()) ? rlm@45: this.getUTCFullYear() + '-' + rlm@45: f(this.getUTCMonth() + 1) + '-' + rlm@45: f(this.getUTCDate()) + 'T' + rlm@45: f(this.getUTCHours()) + ':' + rlm@45: f(this.getUTCMinutes()) + ':' + rlm@45: f(this.getUTCSeconds()) + 'Z' : null; rlm@45: }; rlm@45: rlm@45: String.prototype.toJSON = rlm@45: Number.prototype.toJSON = rlm@45: Boolean.prototype.toJSON = function (key) { rlm@45: return this.valueOf(); rlm@45: }; rlm@45: } rlm@45: rlm@45: var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, rlm@45: escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, rlm@45: gap, rlm@45: indent, rlm@45: meta = { // table of character substitutions rlm@45: '\b': '\\b', rlm@45: '\t': '\\t', rlm@45: '\n': '\\n', rlm@45: '\f': '\\f', rlm@45: '\r': '\\r', rlm@45: '"' : '\\"', rlm@45: '\\': '\\\\' rlm@45: }, rlm@45: rep; rlm@45: rlm@45: rlm@45: function quote(string) { rlm@45: rlm@45: // If the string contains no control characters, no quote characters, and no rlm@45: // backslash characters, then we can safely slap some quotes around it. rlm@45: // Otherwise we must also replace the offending characters with safe escape rlm@45: // sequences. rlm@45: rlm@45: escapable.lastIndex = 0; rlm@45: return escapable.test(string) ? rlm@45: '"' + string.replace(escapable, function (a) { rlm@45: var c = meta[a]; rlm@45: return typeof c === 'string' ? c : rlm@45: '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); rlm@45: }) + '"' : rlm@45: '"' + string + '"'; rlm@45: } rlm@45: rlm@45: rlm@45: function str(key, holder) { rlm@45: rlm@45: // Produce a string from holder[key]. rlm@45: rlm@45: var i, // The loop counter. rlm@45: k, // The member key. rlm@45: v, // The member value. rlm@45: length, rlm@45: mind = gap, rlm@45: partial, rlm@45: value = holder[key]; rlm@45: rlm@45: // If the value has a toJSON method, call it to obtain a replacement value. rlm@45: rlm@45: if (value && typeof value === 'object' && rlm@45: typeof value.toJSON === 'function') { rlm@45: value = value.toJSON(key); rlm@45: } rlm@45: rlm@45: // If we were called with a replacer function, then call the replacer to rlm@45: // obtain a replacement value. rlm@45: rlm@45: if (typeof rep === 'function') { rlm@45: value = rep.call(holder, key, value); rlm@45: } rlm@45: rlm@45: // What happens next depends on the value's type. rlm@45: rlm@45: switch (typeof value) { rlm@45: case 'string': rlm@45: return quote(value); rlm@45: rlm@45: case 'number': rlm@45: rlm@45: // JSON numbers must be finite. Encode non-finite numbers as null. rlm@45: rlm@45: return isFinite(value) ? String(value) : 'null'; rlm@45: rlm@45: case 'boolean': rlm@45: case 'null': rlm@45: rlm@45: // If the value is a boolean or null, convert it to a string. Note: rlm@45: // typeof null does not produce 'null'. The case is included here in rlm@45: // the remote chance that this gets fixed someday. rlm@45: rlm@45: return String(value); rlm@45: rlm@45: // If the type is 'object', we might be dealing with an object or an array or rlm@45: // null. rlm@45: rlm@45: case 'object': rlm@45: rlm@45: // Due to a specification blunder in ECMAScript, typeof null is 'object', rlm@45: // so watch out for that case. rlm@45: rlm@45: if (!value) { rlm@45: return 'null'; rlm@45: } rlm@45: rlm@45: // Make an array to hold the partial results of stringifying this object value. rlm@45: rlm@45: gap += indent; rlm@45: partial = []; rlm@45: rlm@45: // Is the value an array? rlm@45: rlm@45: if (Object.prototype.toString.apply(value) === '[object Array]') { rlm@45: rlm@45: // The value is an array. Stringify every element. Use null as a placeholder rlm@45: // for non-JSON values. rlm@45: rlm@45: length = value.length; rlm@45: for (i = 0; i < length; i += 1) { rlm@45: partial[i] = str(i, value) || 'null'; rlm@45: } rlm@45: rlm@45: // Join all of the elements together, separated with commas, and wrap them in rlm@45: // brackets. rlm@45: rlm@45: v = partial.length === 0 ? '[]' : rlm@45: gap ? '[\n' + gap + rlm@45: partial.join(',\n' + gap) + '\n' + rlm@45: mind + ']' : rlm@45: '[' + partial.join(',') + ']'; rlm@45: gap = mind; rlm@45: return v; rlm@45: } rlm@45: rlm@45: // If the replacer is an array, use it to select the members to be stringified. rlm@45: rlm@45: if (rep && typeof rep === 'object') { rlm@45: length = rep.length; rlm@45: for (i = 0; i < length; i += 1) { rlm@45: k = rep[i]; rlm@45: if (typeof k === 'string') { rlm@45: v = str(k, value); rlm@45: if (v) { rlm@45: partial.push(quote(k) + (gap ? ': ' : ':') + v); rlm@45: } rlm@45: } rlm@45: } rlm@45: } else { rlm@45: rlm@45: // Otherwise, iterate through all of the keys in the object. rlm@45: rlm@45: for (k in value) { rlm@45: if (Object.hasOwnProperty.call(value, k)) { rlm@45: v = str(k, value); rlm@45: if (v) { rlm@45: partial.push(quote(k) + (gap ? ': ' : ':') + v); rlm@45: } rlm@45: } rlm@45: } rlm@45: } rlm@45: rlm@45: // Join all of the member texts together, separated with commas, rlm@45: // and wrap them in braces. rlm@45: rlm@45: v = partial.length === 0 ? '{}' : rlm@45: gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + rlm@45: mind + '}' : '{' + partial.join(',') + '}'; rlm@45: gap = mind; rlm@45: return v; rlm@45: } rlm@45: } rlm@45: rlm@45: // If the JSON object does not yet have a stringify method, give it one. rlm@45: rlm@45: if (typeof JSON.stringify !== 'function') { rlm@45: JSON.stringify = function (value, replacer, space) { rlm@45: rlm@45: // The stringify method takes a value and an optional replacer, and an optional rlm@45: // space parameter, and returns a JSON text. The replacer can be a function rlm@45: // that can replace values, or an array of strings that will select the keys. rlm@45: // A default replacer method can be provided. Use of the space parameter can rlm@45: // produce text that is more easily readable. rlm@45: rlm@45: var i; rlm@45: gap = ''; rlm@45: indent = ''; rlm@45: rlm@45: // If the space parameter is a number, make an indent string containing that rlm@45: // many spaces. rlm@45: rlm@45: if (typeof space === 'number') { rlm@45: for (i = 0; i < space; i += 1) { rlm@45: indent += ' '; rlm@45: } rlm@45: rlm@45: // If the space parameter is a string, it will be used as the indent string. rlm@45: rlm@45: } else if (typeof space === 'string') { rlm@45: indent = space; rlm@45: } rlm@45: rlm@45: // If there is a replacer, it must be a function or an array. rlm@45: // Otherwise, throw an error. rlm@45: rlm@45: rep = replacer; rlm@45: if (replacer && typeof replacer !== 'function' && rlm@45: (typeof replacer !== 'object' || rlm@45: typeof replacer.length !== 'number')) { rlm@45: throw new Error('JSON.stringify'); rlm@45: } rlm@45: rlm@45: // Make a fake root object containing our value under the key of ''. rlm@45: // Return the result of stringifying the value. rlm@45: rlm@45: return str('', {'': value}); rlm@45: }; rlm@45: } rlm@45: rlm@45: rlm@45: // If the JSON object does not yet have a parse method, give it one. rlm@45: rlm@45: if (typeof JSON.parse !== 'function') { rlm@45: JSON.parse = function (text, reviver) { rlm@45: rlm@45: // The parse method takes a text and an optional reviver function, and returns rlm@45: // a JavaScript value if the text is a valid JSON text. rlm@45: rlm@45: var j; rlm@45: rlm@45: function walk(holder, key) { rlm@45: rlm@45: // The walk method is used to recursively walk the resulting structure so rlm@45: // that modifications can be made. rlm@45: rlm@45: var k, v, value = holder[key]; rlm@45: if (value && typeof value === 'object') { rlm@45: for (k in value) { rlm@45: if (Object.hasOwnProperty.call(value, k)) { rlm@45: v = walk(value, k); rlm@45: if (v !== undefined) { rlm@45: value[k] = v; rlm@45: } else { rlm@45: delete value[k]; rlm@45: } rlm@45: } rlm@45: } rlm@45: } rlm@45: return reviver.call(holder, key, value); rlm@45: } rlm@45: rlm@45: rlm@45: // Parsing happens in four stages. In the first stage, we replace certain rlm@45: // Unicode characters with escape sequences. JavaScript handles many characters rlm@45: // incorrectly, either silently deleting them, or treating them as line endings. rlm@45: rlm@45: cx.lastIndex = 0; rlm@45: if (cx.test(text)) { rlm@45: text = text.replace(cx, function (a) { rlm@45: return '\\u' + rlm@45: ('0000' + a.charCodeAt(0).toString(16)).slice(-4); rlm@45: }); rlm@45: } rlm@45: rlm@45: // In the second stage, we run the text against regular expressions that look rlm@45: // for non-JSON patterns. We are especially concerned with '()' and 'new' rlm@45: // because they can cause invocation, and '=' because it can cause mutation. rlm@45: // But just to be safe, we want to reject all unexpected forms. rlm@45: rlm@45: // We split the second stage into 4 regexp operations in order to work around rlm@45: // crippling inefficiencies in IE's and Safari's regexp engines. First we rlm@45: // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we rlm@45: // replace all simple value tokens with ']' characters. Third, we delete all rlm@45: // open brackets that follow a colon or comma or that begin the text. Finally, rlm@45: // we look to see that the remaining characters are only whitespace or ']' or rlm@45: // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval. rlm@45: rlm@45: if (/^[\],:{}\s]*$/. rlm@45: test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@'). rlm@45: replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'). rlm@45: replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { rlm@45: rlm@45: // In the third stage we use the eval function to compile the text into a rlm@45: // JavaScript structure. The '{' operator is subject to a syntactic ambiguity rlm@45: // in JavaScript: it can begin a block or an object literal. We wrap the text rlm@45: // in parens to eliminate the ambiguity. rlm@45: rlm@45: j = eval('(' + text + ')'); rlm@45: rlm@45: // In the optional fourth stage, we recursively walk the new structure, passing rlm@45: // each name/value pair to a reviver function for possible transformation. rlm@45: rlm@45: return typeof reviver === 'function' ? rlm@45: walk({'': j}, '') : j; rlm@45: } rlm@45: rlm@45: // If the text is not JSON parseable, then a SyntaxError is thrown. rlm@45: rlm@45: throw new SyntaxError('JSON.parse'); rlm@45: }; rlm@45: } rlm@45: }());