Chris@5
|
1 // json2.js
|
Chris@5
|
2 // 2016-10-28
|
Chris@5
|
3 // Public Domain.
|
Chris@5
|
4 // NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
|
Chris@5
|
5 // See http://www.JSON.org/js.html
|
Chris@5
|
6 // This code should be minified before deployment.
|
Chris@5
|
7 // See http://javascript.crockford.com/jsmin.html
|
Chris@5
|
8
|
Chris@5
|
9 // USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
|
Chris@5
|
10 // NOT CONTROL.
|
Chris@5
|
11
|
Chris@5
|
12 // This file creates a global JSON object containing two methods: stringify
|
Chris@5
|
13 // and parse. This file provides the ES5 JSON capability to ES3 systems.
|
Chris@5
|
14 // If a project might run on IE8 or earlier, then this file should be included.
|
Chris@5
|
15 // This file does nothing on ES5 systems.
|
Chris@5
|
16
|
Chris@5
|
17 // JSON.stringify(value, replacer, space)
|
Chris@5
|
18 // value any JavaScript value, usually an object or array.
|
Chris@5
|
19 // replacer an optional parameter that determines how object
|
Chris@5
|
20 // values are stringified for objects. It can be a
|
Chris@5
|
21 // function or an array of strings.
|
Chris@5
|
22 // space an optional parameter that specifies the indentation
|
Chris@5
|
23 // of nested structures. If it is omitted, the text will
|
Chris@5
|
24 // be packed without extra whitespace. If it is a number,
|
Chris@5
|
25 // it will specify the number of spaces to indent at each
|
Chris@5
|
26 // level. If it is a string (such as "\t" or " "),
|
Chris@5
|
27 // it contains the characters used to indent at each level.
|
Chris@5
|
28 // This method produces a JSON text from a JavaScript value.
|
Chris@5
|
29 // When an object value is found, if the object contains a toJSON
|
Chris@5
|
30 // method, its toJSON method will be called and the result will be
|
Chris@5
|
31 // stringified. A toJSON method does not serialize: it returns the
|
Chris@5
|
32 // value represented by the name/value pair that should be serialized,
|
Chris@5
|
33 // or undefined if nothing should be serialized. The toJSON method
|
Chris@5
|
34 // will be passed the key associated with the value, and this will be
|
Chris@5
|
35 // bound to the value.
|
Chris@5
|
36
|
Chris@5
|
37 // For example, this would serialize Dates as ISO strings.
|
Chris@5
|
38
|
Chris@5
|
39 // Date.prototype.toJSON = function (key) {
|
Chris@5
|
40 // function f(n) {
|
Chris@5
|
41 // // Format integers to have at least two digits.
|
Chris@5
|
42 // return (n < 10)
|
Chris@5
|
43 // ? "0" + n
|
Chris@5
|
44 // : n;
|
Chris@5
|
45 // }
|
Chris@5
|
46 // return this.getUTCFullYear() + "-" +
|
Chris@5
|
47 // f(this.getUTCMonth() + 1) + "-" +
|
Chris@5
|
48 // f(this.getUTCDate()) + "T" +
|
Chris@5
|
49 // f(this.getUTCHours()) + ":" +
|
Chris@5
|
50 // f(this.getUTCMinutes()) + ":" +
|
Chris@5
|
51 // f(this.getUTCSeconds()) + "Z";
|
Chris@5
|
52 // };
|
Chris@5
|
53
|
Chris@5
|
54 // You can provide an optional replacer method. It will be passed the
|
Chris@5
|
55 // key and value of each member, with this bound to the containing
|
Chris@5
|
56 // object. The value that is returned from your method will be
|
Chris@5
|
57 // serialized. If your method returns undefined, then the member will
|
Chris@5
|
58 // be excluded from the serialization.
|
Chris@5
|
59
|
Chris@5
|
60 // If the replacer parameter is an array of strings, then it will be
|
Chris@5
|
61 // used to select the members to be serialized. It filters the results
|
Chris@5
|
62 // such that only members with keys listed in the replacer array are
|
Chris@5
|
63 // stringified.
|
Chris@5
|
64
|
Chris@5
|
65 // Values that do not have JSON representations, such as undefined or
|
Chris@5
|
66 // functions, will not be serialized. Such values in objects will be
|
Chris@5
|
67 // dropped; in arrays they will be replaced with null. You can use
|
Chris@5
|
68 // a replacer function to replace those with JSON values.
|
Chris@5
|
69
|
Chris@5
|
70 // JSON.stringify(undefined) returns undefined.
|
Chris@5
|
71
|
Chris@5
|
72 // The optional space parameter produces a stringification of the
|
Chris@5
|
73 // value that is filled with line breaks and indentation to make it
|
Chris@5
|
74 // easier to read.
|
Chris@5
|
75
|
Chris@5
|
76 // If the space parameter is a non-empty string, then that string will
|
Chris@5
|
77 // be used for indentation. If the space parameter is a number, then
|
Chris@5
|
78 // the indentation will be that many spaces.
|
Chris@5
|
79
|
Chris@5
|
80 // Example:
|
Chris@5
|
81
|
Chris@5
|
82 // text = JSON.stringify(["e", {pluribus: "unum"}]);
|
Chris@5
|
83 // // text is '["e",{"pluribus":"unum"}]'
|
Chris@5
|
84
|
Chris@5
|
85 // text = JSON.stringify(["e", {pluribus: "unum"}], null, "\t");
|
Chris@5
|
86 // // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
|
Chris@5
|
87
|
Chris@5
|
88 // text = JSON.stringify([new Date()], function (key, value) {
|
Chris@5
|
89 // return this[key] instanceof Date
|
Chris@5
|
90 // ? "Date(" + this[key] + ")"
|
Chris@5
|
91 // : value;
|
Chris@5
|
92 // });
|
Chris@5
|
93 // // text is '["Date(---current time---)"]'
|
Chris@5
|
94
|
Chris@5
|
95 // JSON.parse(text, reviver)
|
Chris@5
|
96 // This method parses a JSON text to produce an object or array.
|
Chris@5
|
97 // It can throw a SyntaxError exception.
|
Chris@5
|
98
|
Chris@5
|
99 // The optional reviver parameter is a function that can filter and
|
Chris@5
|
100 // transform the results. It receives each of the keys and values,
|
Chris@5
|
101 // and its return value is used instead of the original value.
|
Chris@5
|
102 // If it returns what it received, then the structure is not modified.
|
Chris@5
|
103 // If it returns undefined then the member is deleted.
|
Chris@5
|
104
|
Chris@5
|
105 // Example:
|
Chris@5
|
106
|
Chris@5
|
107 // // Parse the text. Values that look like ISO date strings will
|
Chris@5
|
108 // // be converted to Date objects.
|
Chris@5
|
109
|
Chris@5
|
110 // myData = JSON.parse(text, function (key, value) {
|
Chris@5
|
111 // var a;
|
Chris@5
|
112 // if (typeof value === "string") {
|
Chris@5
|
113 // a =
|
Chris@5
|
114 // /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
|
Chris@5
|
115 // if (a) {
|
Chris@5
|
116 // return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
|
Chris@5
|
117 // +a[5], +a[6]));
|
Chris@5
|
118 // }
|
Chris@5
|
119 // }
|
Chris@5
|
120 // return value;
|
Chris@5
|
121 // });
|
Chris@5
|
122
|
Chris@5
|
123 // myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
|
Chris@5
|
124 // var d;
|
Chris@5
|
125 // if (typeof value === "string" &&
|
Chris@5
|
126 // value.slice(0, 5) === "Date(" &&
|
Chris@5
|
127 // value.slice(-1) === ")") {
|
Chris@5
|
128 // d = new Date(value.slice(5, -1));
|
Chris@5
|
129 // if (d) {
|
Chris@5
|
130 // return d;
|
Chris@5
|
131 // }
|
Chris@5
|
132 // }
|
Chris@5
|
133 // return value;
|
Chris@5
|
134 // });
|
Chris@5
|
135
|
Chris@5
|
136 // This is a reference implementation. You are free to copy, modify, or
|
Chris@5
|
137 // redistribute.
|
Chris@5
|
138
|
Chris@5
|
139 /*jslint
|
Chris@5
|
140 eval, for, this
|
Chris@5
|
141 */
|
Chris@5
|
142
|
Chris@5
|
143 /*property
|
Chris@5
|
144 JSON, apply, call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
|
Chris@5
|
145 getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
|
Chris@5
|
146 lastIndex, length, parse, prototype, push, replace, slice, stringify,
|
Chris@5
|
147 test, toJSON, toString, valueOf
|
Chris@5
|
148 */
|
Chris@5
|
149
|
Chris@5
|
150
|
Chris@5
|
151 // Create a JSON object only if one does not already exist. We create the
|
Chris@5
|
152 // methods in a closure to avoid creating global variables.
|
Chris@5
|
153
|
Chris@5
|
154 if (typeof JSON !== "object") {
|
Chris@5
|
155 JSON = {};
|
Chris@5
|
156 }
|
Chris@5
|
157
|
Chris@5
|
158 (function () {
|
Chris@5
|
159 "use strict";
|
Chris@5
|
160
|
Chris@5
|
161 var rx_one = /^[\],:{}\s]*$/;
|
Chris@5
|
162 var rx_two = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g;
|
Chris@5
|
163 var rx_three = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
|
Chris@5
|
164 var rx_four = /(?:^|:|,)(?:\s*\[)+/g;
|
Chris@5
|
165 var rx_escapable = /[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
|
Chris@5
|
166 var rx_dangerous = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
|
Chris@5
|
167
|
Chris@5
|
168 function f(n) {
|
Chris@5
|
169 // Format integers to have at least two digits.
|
Chris@5
|
170 return n < 10
|
Chris@5
|
171 ? "0" + n
|
Chris@5
|
172 : n;
|
Chris@5
|
173 }
|
Chris@5
|
174
|
Chris@5
|
175 function this_value() {
|
Chris@5
|
176 return this.valueOf();
|
Chris@5
|
177 }
|
Chris@5
|
178
|
Chris@5
|
179 if (typeof Date.prototype.toJSON !== "function") {
|
Chris@5
|
180
|
Chris@5
|
181 Date.prototype.toJSON = function () {
|
Chris@5
|
182
|
Chris@5
|
183 return isFinite(this.valueOf())
|
Chris@5
|
184 ? this.getUTCFullYear() + "-" +
|
Chris@5
|
185 f(this.getUTCMonth() + 1) + "-" +
|
Chris@5
|
186 f(this.getUTCDate()) + "T" +
|
Chris@5
|
187 f(this.getUTCHours()) + ":" +
|
Chris@5
|
188 f(this.getUTCMinutes()) + ":" +
|
Chris@5
|
189 f(this.getUTCSeconds()) + "Z"
|
Chris@5
|
190 : null;
|
Chris@5
|
191 };
|
Chris@5
|
192
|
Chris@5
|
193 Boolean.prototype.toJSON = this_value;
|
Chris@5
|
194 Number.prototype.toJSON = this_value;
|
Chris@5
|
195 String.prototype.toJSON = this_value;
|
Chris@5
|
196 }
|
Chris@5
|
197
|
Chris@5
|
198 var gap;
|
Chris@5
|
199 var indent;
|
Chris@5
|
200 var meta;
|
Chris@5
|
201 var rep;
|
Chris@5
|
202
|
Chris@5
|
203
|
Chris@5
|
204 function quote(string) {
|
Chris@5
|
205
|
Chris@5
|
206 // If the string contains no control characters, no quote characters, and no
|
Chris@5
|
207 // backslash characters, then we can safely slap some quotes around it.
|
Chris@5
|
208 // Otherwise we must also replace the offending characters with safe escape
|
Chris@5
|
209 // sequences.
|
Chris@5
|
210
|
Chris@5
|
211 rx_escapable.lastIndex = 0;
|
Chris@5
|
212 return rx_escapable.test(string)
|
Chris@5
|
213 ? "\"" + string.replace(rx_escapable, function (a) {
|
Chris@5
|
214 var c = meta[a];
|
Chris@5
|
215 return typeof c === "string"
|
Chris@5
|
216 ? c
|
Chris@5
|
217 : "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4);
|
Chris@5
|
218 }) + "\""
|
Chris@5
|
219 : "\"" + string + "\"";
|
Chris@5
|
220 }
|
Chris@5
|
221
|
Chris@5
|
222
|
Chris@5
|
223 function str(key, holder) {
|
Chris@5
|
224
|
Chris@5
|
225 // Produce a string from holder[key].
|
Chris@5
|
226
|
Chris@5
|
227 var i; // The loop counter.
|
Chris@5
|
228 var k; // The member key.
|
Chris@5
|
229 var v; // The member value.
|
Chris@5
|
230 var length;
|
Chris@5
|
231 var mind = gap;
|
Chris@5
|
232 var partial;
|
Chris@5
|
233 var value = holder[key];
|
Chris@5
|
234
|
Chris@5
|
235 // If the value has a toJSON method, call it to obtain a replacement value.
|
Chris@5
|
236
|
Chris@5
|
237 if (value && typeof value === "object" &&
|
Chris@5
|
238 typeof value.toJSON === "function") {
|
Chris@5
|
239 value = value.toJSON(key);
|
Chris@5
|
240 }
|
Chris@5
|
241
|
Chris@5
|
242 // If we were called with a replacer function, then call the replacer to
|
Chris@5
|
243 // obtain a replacement value.
|
Chris@5
|
244
|
Chris@5
|
245 if (typeof rep === "function") {
|
Chris@5
|
246 value = rep.call(holder, key, value);
|
Chris@5
|
247 }
|
Chris@5
|
248
|
Chris@5
|
249 // What happens next depends on the value's type.
|
Chris@5
|
250
|
Chris@5
|
251 switch (typeof value) {
|
Chris@5
|
252 case "string":
|
Chris@5
|
253 return quote(value);
|
Chris@5
|
254
|
Chris@5
|
255 case "number":
|
Chris@5
|
256
|
Chris@5
|
257 // JSON numbers must be finite. Encode non-finite numbers as null.
|
Chris@5
|
258
|
Chris@5
|
259 return isFinite(value)
|
Chris@5
|
260 ? String(value)
|
Chris@5
|
261 : "null";
|
Chris@5
|
262
|
Chris@5
|
263 case "boolean":
|
Chris@5
|
264 case "null":
|
Chris@5
|
265
|
Chris@5
|
266 // If the value is a boolean or null, convert it to a string. Note:
|
Chris@5
|
267 // typeof null does not produce "null". The case is included here in
|
Chris@5
|
268 // the remote chance that this gets fixed someday.
|
Chris@5
|
269
|
Chris@5
|
270 return String(value);
|
Chris@5
|
271
|
Chris@5
|
272 // If the type is "object", we might be dealing with an object or an array or
|
Chris@5
|
273 // null.
|
Chris@5
|
274
|
Chris@5
|
275 case "object":
|
Chris@5
|
276
|
Chris@5
|
277 // Due to a specification blunder in ECMAScript, typeof null is "object",
|
Chris@5
|
278 // so watch out for that case.
|
Chris@5
|
279
|
Chris@5
|
280 if (!value) {
|
Chris@5
|
281 return "null";
|
Chris@5
|
282 }
|
Chris@5
|
283
|
Chris@5
|
284 // Make an array to hold the partial results of stringifying this object value.
|
Chris@5
|
285
|
Chris@5
|
286 gap += indent;
|
Chris@5
|
287 partial = [];
|
Chris@5
|
288
|
Chris@5
|
289 // Is the value an array?
|
Chris@5
|
290
|
Chris@5
|
291 if (Object.prototype.toString.apply(value) === "[object Array]") {
|
Chris@5
|
292
|
Chris@5
|
293 // The value is an array. Stringify every element. Use null as a placeholder
|
Chris@5
|
294 // for non-JSON values.
|
Chris@5
|
295
|
Chris@5
|
296 length = value.length;
|
Chris@5
|
297 for (i = 0; i < length; i += 1) {
|
Chris@5
|
298 partial[i] = str(i, value) || "null";
|
Chris@5
|
299 }
|
Chris@5
|
300
|
Chris@5
|
301 // Join all of the elements together, separated with commas, and wrap them in
|
Chris@5
|
302 // brackets.
|
Chris@5
|
303
|
Chris@5
|
304 v = partial.length === 0
|
Chris@5
|
305 ? "[]"
|
Chris@5
|
306 : gap
|
Chris@5
|
307 ? "[\n" + gap + partial.join(",\n" + gap) + "\n" + mind + "]"
|
Chris@5
|
308 : "[" + partial.join(",") + "]";
|
Chris@5
|
309 gap = mind;
|
Chris@5
|
310 return v;
|
Chris@5
|
311 }
|
Chris@5
|
312
|
Chris@5
|
313 // If the replacer is an array, use it to select the members to be stringified.
|
Chris@5
|
314
|
Chris@5
|
315 if (rep && typeof rep === "object") {
|
Chris@5
|
316 length = rep.length;
|
Chris@5
|
317 for (i = 0; i < length; i += 1) {
|
Chris@5
|
318 if (typeof rep[i] === "string") {
|
Chris@5
|
319 k = rep[i];
|
Chris@5
|
320 v = str(k, value);
|
Chris@5
|
321 if (v) {
|
Chris@5
|
322 partial.push(quote(k) + (
|
Chris@5
|
323 gap
|
Chris@5
|
324 ? ": "
|
Chris@5
|
325 : ":"
|
Chris@5
|
326 ) + v);
|
Chris@5
|
327 }
|
Chris@5
|
328 }
|
Chris@5
|
329 }
|
Chris@5
|
330 } else {
|
Chris@5
|
331
|
Chris@5
|
332 // Otherwise, iterate through all of the keys in the object.
|
Chris@5
|
333
|
Chris@5
|
334 for (k in value) {
|
Chris@5
|
335 if (Object.prototype.hasOwnProperty.call(value, k)) {
|
Chris@5
|
336 v = str(k, value);
|
Chris@5
|
337 if (v) {
|
Chris@5
|
338 partial.push(quote(k) + (
|
Chris@5
|
339 gap
|
Chris@5
|
340 ? ": "
|
Chris@5
|
341 : ":"
|
Chris@5
|
342 ) + v);
|
Chris@5
|
343 }
|
Chris@5
|
344 }
|
Chris@5
|
345 }
|
Chris@5
|
346 }
|
Chris@5
|
347
|
Chris@5
|
348 // Join all of the member texts together, separated with commas,
|
Chris@5
|
349 // and wrap them in braces.
|
Chris@5
|
350
|
Chris@5
|
351 v = partial.length === 0
|
Chris@5
|
352 ? "{}"
|
Chris@5
|
353 : gap
|
Chris@5
|
354 ? "{\n" + gap + partial.join(",\n" + gap) + "\n" + mind + "}"
|
Chris@5
|
355 : "{" + partial.join(",") + "}";
|
Chris@5
|
356 gap = mind;
|
Chris@5
|
357 return v;
|
Chris@5
|
358 }
|
Chris@5
|
359 }
|
Chris@5
|
360
|
Chris@5
|
361 // If the JSON object does not yet have a stringify method, give it one.
|
Chris@5
|
362
|
Chris@5
|
363 if (typeof JSON.stringify !== "function") {
|
Chris@5
|
364 meta = { // table of character substitutions
|
Chris@5
|
365 "\b": "\\b",
|
Chris@5
|
366 "\t": "\\t",
|
Chris@5
|
367 "\n": "\\n",
|
Chris@5
|
368 "\f": "\\f",
|
Chris@5
|
369 "\r": "\\r",
|
Chris@5
|
370 "\"": "\\\"",
|
Chris@5
|
371 "\\": "\\\\"
|
Chris@5
|
372 };
|
Chris@5
|
373 JSON.stringify = function (value, replacer, space) {
|
Chris@5
|
374
|
Chris@5
|
375 // The stringify method takes a value and an optional replacer, and an optional
|
Chris@5
|
376 // space parameter, and returns a JSON text. The replacer can be a function
|
Chris@5
|
377 // that can replace values, or an array of strings that will select the keys.
|
Chris@5
|
378 // A default replacer method can be provided. Use of the space parameter can
|
Chris@5
|
379 // produce text that is more easily readable.
|
Chris@5
|
380
|
Chris@5
|
381 var i;
|
Chris@5
|
382 gap = "";
|
Chris@5
|
383 indent = "";
|
Chris@5
|
384
|
Chris@5
|
385 // If the space parameter is a number, make an indent string containing that
|
Chris@5
|
386 // many spaces.
|
Chris@5
|
387
|
Chris@5
|
388 if (typeof space === "number") {
|
Chris@5
|
389 for (i = 0; i < space; i += 1) {
|
Chris@5
|
390 indent += " ";
|
Chris@5
|
391 }
|
Chris@5
|
392
|
Chris@5
|
393 // If the space parameter is a string, it will be used as the indent string.
|
Chris@5
|
394
|
Chris@5
|
395 } else if (typeof space === "string") {
|
Chris@5
|
396 indent = space;
|
Chris@5
|
397 }
|
Chris@5
|
398
|
Chris@5
|
399 // If there is a replacer, it must be a function or an array.
|
Chris@5
|
400 // Otherwise, throw an error.
|
Chris@5
|
401
|
Chris@5
|
402 rep = replacer;
|
Chris@5
|
403 if (replacer && typeof replacer !== "function" &&
|
Chris@5
|
404 (typeof replacer !== "object" ||
|
Chris@5
|
405 typeof replacer.length !== "number")) {
|
Chris@5
|
406 throw new Error("JSON.stringify");
|
Chris@5
|
407 }
|
Chris@5
|
408
|
Chris@5
|
409 // Make a fake root object containing our value under the key of "".
|
Chris@5
|
410 // Return the result of stringifying the value.
|
Chris@5
|
411
|
Chris@5
|
412 return str("", {"": value});
|
Chris@5
|
413 };
|
Chris@5
|
414 }
|
Chris@5
|
415
|
Chris@5
|
416
|
Chris@5
|
417 // If the JSON object does not yet have a parse method, give it one.
|
Chris@5
|
418
|
Chris@5
|
419 if (typeof JSON.parse !== "function") {
|
Chris@5
|
420 JSON.parse = function (text, reviver) {
|
Chris@5
|
421
|
Chris@5
|
422 // The parse method takes a text and an optional reviver function, and returns
|
Chris@5
|
423 // a JavaScript value if the text is a valid JSON text.
|
Chris@5
|
424
|
Chris@5
|
425 var j;
|
Chris@5
|
426
|
Chris@5
|
427 function walk(holder, key) {
|
Chris@5
|
428
|
Chris@5
|
429 // The walk method is used to recursively walk the resulting structure so
|
Chris@5
|
430 // that modifications can be made.
|
Chris@5
|
431
|
Chris@5
|
432 var k;
|
Chris@5
|
433 var v;
|
Chris@5
|
434 var value = holder[key];
|
Chris@5
|
435 if (value && typeof value === "object") {
|
Chris@5
|
436 for (k in value) {
|
Chris@5
|
437 if (Object.prototype.hasOwnProperty.call(value, k)) {
|
Chris@5
|
438 v = walk(value, k);
|
Chris@5
|
439 if (v !== undefined) {
|
Chris@5
|
440 value[k] = v;
|
Chris@5
|
441 } else {
|
Chris@5
|
442 delete value[k];
|
Chris@5
|
443 }
|
Chris@5
|
444 }
|
Chris@5
|
445 }
|
Chris@5
|
446 }
|
Chris@5
|
447 return reviver.call(holder, key, value);
|
Chris@5
|
448 }
|
Chris@5
|
449
|
Chris@5
|
450
|
Chris@5
|
451 // Parsing happens in four stages. In the first stage, we replace certain
|
Chris@5
|
452 // Unicode characters with escape sequences. JavaScript handles many characters
|
Chris@5
|
453 // incorrectly, either silently deleting them, or treating them as line endings.
|
Chris@5
|
454
|
Chris@5
|
455 text = String(text);
|
Chris@5
|
456 rx_dangerous.lastIndex = 0;
|
Chris@5
|
457 if (rx_dangerous.test(text)) {
|
Chris@5
|
458 text = text.replace(rx_dangerous, function (a) {
|
Chris@5
|
459 return "\\u" +
|
Chris@5
|
460 ("0000" + a.charCodeAt(0).toString(16)).slice(-4);
|
Chris@5
|
461 });
|
Chris@5
|
462 }
|
Chris@5
|
463
|
Chris@5
|
464 // In the second stage, we run the text against regular expressions that look
|
Chris@5
|
465 // for non-JSON patterns. We are especially concerned with "()" and "new"
|
Chris@5
|
466 // because they can cause invocation, and "=" because it can cause mutation.
|
Chris@5
|
467 // But just to be safe, we want to reject all unexpected forms.
|
Chris@5
|
468
|
Chris@5
|
469 // We split the second stage into 4 regexp operations in order to work around
|
Chris@5
|
470 // crippling inefficiencies in IE's and Safari's regexp engines. First we
|
Chris@5
|
471 // replace the JSON backslash pairs with "@" (a non-JSON character). Second, we
|
Chris@5
|
472 // replace all simple value tokens with "]" characters. Third, we delete all
|
Chris@5
|
473 // open brackets that follow a colon or comma or that begin the text. Finally,
|
Chris@5
|
474 // we look to see that the remaining characters are only whitespace or "]" or
|
Chris@5
|
475 // "," or ":" or "{" or "}". If that is so, then the text is safe for eval.
|
Chris@5
|
476
|
Chris@5
|
477 if (
|
Chris@5
|
478 rx_one.test(
|
Chris@5
|
479 text
|
Chris@5
|
480 .replace(rx_two, "@")
|
Chris@5
|
481 .replace(rx_three, "]")
|
Chris@5
|
482 .replace(rx_four, "")
|
Chris@5
|
483 )
|
Chris@5
|
484 ) {
|
Chris@5
|
485
|
Chris@5
|
486 // In the third stage we use the eval function to compile the text into a
|
Chris@5
|
487 // JavaScript structure. The "{" operator is subject to a syntactic ambiguity
|
Chris@5
|
488 // in JavaScript: it can begin a block or an object literal. We wrap the text
|
Chris@5
|
489 // in parens to eliminate the ambiguity.
|
Chris@5
|
490
|
Chris@5
|
491 j = eval("(" + text + ")");
|
Chris@5
|
492
|
Chris@5
|
493 // In the optional fourth stage, we recursively walk the new structure, passing
|
Chris@5
|
494 // each name/value pair to a reviver function for possible transformation.
|
Chris@5
|
495
|
Chris@5
|
496 return (typeof reviver === "function")
|
Chris@5
|
497 ? walk({"": j}, "")
|
Chris@5
|
498 : j;
|
Chris@5
|
499 }
|
Chris@5
|
500
|
Chris@5
|
501 // If the text is not JSON parseable, then a SyntaxError is thrown.
|
Chris@5
|
502
|
Chris@5
|
503 throw new SyntaxError("JSON.parse");
|
Chris@5
|
504 };
|
Chris@5
|
505 }
|
Chris@5
|
506 }());
|