2
http://www.JSON.org/json2.js
7
NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
9
See http://www.JSON.org/js.html
11
This file creates a global JSON object containing two methods: stringify
15
JSON.stringify(value, replacer, space)
16
value any JavaScript value, usually an object or array.
18
replacer an optional parameter that determines how object
19
values are stringified for objects without a toJSON
20
method. It can be a function or an array.
22
space an optional parameter that specifies the indentation
23
of nested structures. If it is omitted, the text will
24
be packed without extra whitespace. If it is a number,
25
it will specify the number of spaces to indent at each
26
level. If it is a string (such as '\t' or ' '),
27
it contains the characters used to indent at each level.
29
This method produces a JSON text from a JavaScript value.
31
When an object value is found, if the object contains a toJSON
32
method, its toJSON method will be called and the result will be
33
stringified. A toJSON method does not serialize: it returns the
34
value represented by the name/value pair that should be serialized,
35
or undefined if nothing should be serialized. The toJSON method
36
will be passed the key associated with the value, and this will be
37
bound to the object holding the key.
39
For example, this would serialize Dates as ISO strings.
41
Date.prototype.toJSON = function (key) {
43
// Format integers to have at least two digits.
44
return n < 10 ? '0' + n : n;
47
return this.getUTCFullYear() + '-' +
48
f(this.getUTCMonth() + 1) + '-' +
49
f(this.getUTCDate()) + 'T' +
50
f(this.getUTCHours()) + ':' +
51
f(this.getUTCMinutes()) + ':' +
52
f(this.getUTCSeconds()) + 'Z';
55
You can provide an optional replacer method. It will be passed the
56
key and value of each member, with this bound to the containing
57
object. The value that is returned from your method will be
58
serialized. If your method returns undefined, then the member will
59
be excluded from the serialization.
61
If the replacer parameter is an array, then it will be used to
62
select the members to be serialized. It filters the results such
63
that only members with keys listed in the replacer array are
66
Values that do not have JSON representations, such as undefined or
67
functions, will not be serialized. Such values in objects will be
68
dropped; in arrays they will be replaced with null. You can use
69
a replacer function to replace those with JSON values.
70
JSON.stringify(undefined) returns undefined.
72
The optional space parameter produces a stringification of the
73
value that is filled with line breaks and indentation to make it
76
If the space parameter is a non-empty string, then that string will
77
be used for indentation. If the space parameter is a number, then
78
then indentation will be that many spaces.
82
text = JSON.stringify(['e', {pluribus: 'unum'}]);
83
// text is '["e",{"pluribus":"unum"}]'
86
text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
87
// text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
89
text = JSON.stringify([new Date()], function (key, value) {
90
return this[key] instanceof Date ?
91
'Date(' + this[key] + ')' : value;
93
// text is '["Date(---current time---)"]'
96
JSON.parse(text, reviver)
97
This method parses a JSON text to produce an object or array.
98
It can throw a SyntaxError exception.
100
The optional reviver parameter is a function that can filter and
101
transform the results. It receives each of the keys and values,
102
and its return value is used instead of the original value.
103
If it returns what it received, then the structure is not modified.
104
If it returns undefined then the member is deleted.
108
// Parse the text. Values that look like ISO date strings will
109
// be converted to Date objects.
111
myData = JSON.parse(text, function (key, value) {
113
if (typeof value === 'string') {
115
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
117
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
124
myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
126
if (typeof value === 'string' &&
127
value.slice(0, 5) === 'Date(' &&
128
value.slice(-1) === ')') {
129
d = new Date(value.slice(5, -1));
138
This is a reference implementation. You are free to copy, modify, or
141
This code should be minified before deployment.
142
See http://javascript.crockford.com/jsmin.html
144
USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD THIRD PARTY
145
CODE INTO YOUR PAGES.
148
/*jslint evil: true */
152
/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", call,
153
charCodeAt, getUTCDate, getUTCFullYear, getUTCHours, getUTCMinutes,
154
getUTCMonth, getUTCSeconds, hasOwnProperty, join, lastIndex, length,
155
parse, propertyIsEnumerable, prototype, push, replace, slice, stringify,
156
test, toJSON, toString
161
// Create a JSON object only if one does not already exist. We create the
162
// object in a closure to avoid global variables.
167
// Format integers to have at least two digits.
168
return n < 10 ? '0' + n : n;
171
Date.prototype.toJSON = function (key) {
173
return this.getUTCFullYear() + '-' +
174
f(this.getUTCMonth() + 1) + '-' +
175
f(this.getUTCDate()) + 'T' +
176
f(this.getUTCHours()) + ':' +
177
f(this.getUTCMinutes()) + ':' +
178
f(this.getUTCSeconds()) + 'Z';
181
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
182
escapeable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
185
meta = { // table of character substitutions
197
function quote(string) {
199
// If the string contains no control characters, no quote characters, and no
200
// backslash characters, then we can safely slap some quotes around it.
201
// Otherwise we must also replace the offending characters with safe escape
204
escapeable.lastIndex = 0;
205
return escapeable.test(string) ?
206
'"' + string.replace(escapeable, function (a) {
208
if (typeof c === 'string') {
211
return '\\u' + ('0000' +
212
(+(a.charCodeAt(0))).toString(16)).slice(-4);
218
function str(key, holder) {
220
// Produce a string from holder[key].
222
var i, // The loop counter.
223
k, // The member key.
224
v, // The member value.
230
// If the value has a toJSON method, call it to obtain a replacement value.
232
if (value && typeof value === 'object' &&
233
typeof value.toJSON === 'function') {
234
value = value.toJSON(key);
237
// If we were called with a replacer function, then call the replacer to
238
// obtain a replacement value.
240
if (typeof rep === 'function') {
241
value = rep.call(holder, key, value);
244
// What happens next depends on the value's type.
246
switch (typeof value) {
252
// JSON numbers must be finite. Encode non-finite numbers as null.
254
return isFinite(value) ? String(value) : 'null';
259
// If the value is a boolean or null, convert it to a string. Note:
260
// typeof null does not produce 'null'. The case is included here in
261
// the remote chance that this gets fixed someday.
263
return String(value);
265
// If the type is 'object', we might be dealing with an object or an array or
270
// Due to a specification blunder in ECMAScript, typeof null is 'object',
271
// so watch out for that case.
277
// Make an array to hold the partial results of stringifying this object value.
282
// If the object has a dontEnum length property, we'll treat it as an array.
284
if (typeof value.length === 'number' &&
285
!(value.propertyIsEnumerable('length'))) {
287
// The object is an array. Stringify every element. Use null as a placeholder
288
// for non-JSON values.
290
length = value.length;
291
for (i = 0; i < length; i += 1) {
292
partial[i] = str(i, value) || 'null';
295
// Join all of the elements together, separated with commas, and wrap them in
298
v = partial.length === 0 ? '[]' :
300
partial.join(',\n' + gap) + '\n' +
302
'[' + partial.join(',') + ']';
307
// If the replacer is an array, use it to select the members to be stringified.
309
if (rep && typeof rep === 'object') {
311
for (i = 0; i < length; i += 1) {
313
if (typeof k === 'string') {
314
v = str(k, value, rep);
316
partial.push(quote(k) + (gap ? ': ' : ':') + v);
322
// Otherwise, iterate through all of the keys in the object.
325
if (Object.hasOwnProperty.call(value, k)) {
326
v = str(k, value, rep);
328
partial.push(quote(k) + (gap ? ': ' : ':') + v);
334
// Join all of the member texts together, separated with commas,
335
// and wrap them in braces.
337
v = partial.length === 0 ? '{}' :
339
partial.join(',\n' + gap) + '\n' +
341
'{' + partial.join(',') + '}';
348
// Return the JSON object containing the stringify, parse, and quote methods.
351
stringify: function (value, replacer, space) {
353
// The stringify method takes a value and an optional replacer, and an optional
354
// space parameter, and returns a JSON text. The replacer can be a function
355
// that can replace values, or an array of strings that will select the keys.
356
// A default replacer method can be provided. Use of the space parameter can
357
// produce text that is more easily readable.
363
// If the space parameter is a number, make an indent string containing that
366
if (typeof space === 'number') {
367
for (i = 0; i < space; i += 1) {
371
// If the space parameter is a string, it will be used as the indent string.
373
} else if (typeof space === 'string') {
377
// If there is a replacer, it must be a function or an array.
378
// Otherwise, throw an error.
381
if (replacer && typeof replacer !== 'function' &&
382
(typeof replacer !== 'object' ||
383
typeof replacer.length !== 'number')) {
384
throw new Error('JSON.stringify');
387
// Make a fake root object containing our value under the key of ''.
388
// Return the result of stringifying the value.
390
return str('', {'': value});
394
parse: function (text, reviver) {
396
// The parse method takes a text and an optional reviver function, and returns
397
// a JavaScript value if the text is a valid JSON text.
401
function walk(holder, key) {
403
// The walk method is used to recursively walk the resulting structure so
404
// that modifications can be made.
406
var k, v, value = holder[key];
407
if (value && typeof value === 'object') {
409
if (Object.hasOwnProperty.call(value, k)) {
411
if (v !== undefined) {
419
return reviver.call(holder, key, value);
423
// Parsing happens in four stages. In the first stage, we replace certain
424
// Unicode characters with escape sequences. JavaScript handles many characters
425
// incorrectly, either silently deleting them, or treating them as line endings.
429
text = text.replace(cx, function (a) {
430
return '\\u' + ('0000' +
431
(+(a.charCodeAt(0))).toString(16)).slice(-4);
435
// In the second stage, we run the text against
436
// regular expressions that look for non-JSON patterns. We are especially
437
// concerned with '()' and 'new' because they can cause invocation, and '='
438
// because it can cause mutation. But just to be safe, we want to reject all
441
// We split the second stage into 4 regexp operations in order to work around
442
// crippling inefficiencies in IE's and Safari's regexp engines. First we
443
// replace all backslash pairs with '@' (a non-JSON character). Second, we
444
// replace all simple value tokens with ']' characters. Third, we delete all
445
// open brackets that follow a colon or comma or that begin the text. Finally,
446
// we look to see that the remaining characters are only whitespace or ']' or
447
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
449
if (/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
450
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
451
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
453
// In the third stage we use the eval function to compile the text into a
454
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
455
// in JavaScript: it can begin a block or an object literal. We wrap the text
456
// in parens to eliminate the ambiguity.
458
j = eval('(' + text + ')');
460
// In the optional fourth stage, we recursively walk the new structure, passing
461
// each name/value pair to a reviver function for possible transformation.
463
return typeof reviver === 'function' ?
464
walk({'': j}, '') : j;
467
// If the text is not JSON parseable, then a SyntaxError is thrown.
469
throw new SyntaxError('JSON.parse');