nexus site path corrected
[portal.git] / ecomp-portal-FE / client / bower_components / jqTree / static / bower_components / json3 / lib / json3.js
1 /*! JSON v3.3.2 | http://bestiejs.github.io/json3 | Copyright 2012-2014, Kit Cambridge | http://kit.mit-license.org */
2 ;(function () {
3   // Detect the `define` function exposed by asynchronous module loaders. The
4   // strict `define` check is necessary for compatibility with `r.js`.
5   var isLoader = typeof define === "function" && define.amd;
6
7   // A set of types used to distinguish objects from primitives.
8   var objectTypes = {
9     "function": true,
10     "object": true
11   };
12
13   // Detect the `exports` object exposed by CommonJS implementations.
14   var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports;
15
16   // Use the `global` object exposed by Node (including Browserify via
17   // `insert-module-globals`), Narwhal, and Ringo as the default context,
18   // and the `window` object in browsers. Rhino exports a `global` function
19   // instead.
20   var root = objectTypes[typeof window] && window || this,
21       freeGlobal = freeExports && objectTypes[typeof module] && module && !module.nodeType && typeof global == "object" && global;
22
23   if (freeGlobal && (freeGlobal["global"] === freeGlobal || freeGlobal["window"] === freeGlobal || freeGlobal["self"] === freeGlobal)) {
24     root = freeGlobal;
25   }
26
27   // Public: Initializes JSON 3 using the given `context` object, attaching the
28   // `stringify` and `parse` functions to the specified `exports` object.
29   function runInContext(context, exports) {
30     context || (context = root["Object"]());
31     exports || (exports = root["Object"]());
32
33     // Native constructor aliases.
34     var Number = context["Number"] || root["Number"],
35         String = context["String"] || root["String"],
36         Object = context["Object"] || root["Object"],
37         Date = context["Date"] || root["Date"],
38         SyntaxError = context["SyntaxError"] || root["SyntaxError"],
39         TypeError = context["TypeError"] || root["TypeError"],
40         Math = context["Math"] || root["Math"],
41         nativeJSON = context["JSON"] || root["JSON"];
42
43     // Delegate to the native `stringify` and `parse` implementations.
44     if (typeof nativeJSON == "object" && nativeJSON) {
45       exports.stringify = nativeJSON.stringify;
46       exports.parse = nativeJSON.parse;
47     }
48
49     // Convenience aliases.
50     var objectProto = Object.prototype,
51         getClass = objectProto.toString,
52         isProperty, forEach, undef;
53
54     // Test the `Date#getUTC*` methods. Based on work by @Yaffle.
55     var isExtended = new Date(-3509827334573292);
56     try {
57       // The `getUTCFullYear`, `Month`, and `Date` methods return nonsensical
58       // results for certain dates in Opera >= 10.53.
59       isExtended = isExtended.getUTCFullYear() == -109252 && isExtended.getUTCMonth() === 0 && isExtended.getUTCDate() === 1 &&
60         // Safari < 2.0.2 stores the internal millisecond time value correctly,
61         // but clips the values returned by the date methods to the range of
62         // signed 32-bit integers ([-2 ** 31, 2 ** 31 - 1]).
63         isExtended.getUTCHours() == 10 && isExtended.getUTCMinutes() == 37 && isExtended.getUTCSeconds() == 6 && isExtended.getUTCMilliseconds() == 708;
64     } catch (exception) {}
65
66     // Internal: Determines whether the native `JSON.stringify` and `parse`
67     // implementations are spec-compliant. Based on work by Ken Snyder.
68     function has(name) {
69       if (has[name] !== undef) {
70         // Return cached feature test result.
71         return has[name];
72       }
73       var isSupported;
74       if (name == "bug-string-char-index") {
75         // IE <= 7 doesn't support accessing string characters using square
76         // bracket notation. IE 8 only supports this for primitives.
77         isSupported = "a"[0] != "a";
78       } else if (name == "json") {
79         // Indicates whether both `JSON.stringify` and `JSON.parse` are
80         // supported.
81         isSupported = has("json-stringify") && has("json-parse");
82       } else {
83         var value, serialized = '{"a":[1,true,false,null,"\\u0000\\b\\n\\f\\r\\t"]}';
84         // Test `JSON.stringify`.
85         if (name == "json-stringify") {
86           var stringify = exports.stringify, stringifySupported = typeof stringify == "function" && isExtended;
87           if (stringifySupported) {
88             // A test function object with a custom `toJSON` method.
89             (value = function () {
90               return 1;
91             }).toJSON = value;
92             try {
93               stringifySupported =
94                 // Firefox 3.1b1 and b2 serialize string, number, and boolean
95                 // primitives as object literals.
96                 stringify(0) === "0" &&
97                 // FF 3.1b1, b2, and JSON 2 serialize wrapped primitives as object
98                 // literals.
99                 stringify(new Number()) === "0" &&
100                 stringify(new String()) == '""' &&
101                 // FF 3.1b1, 2 throw an error if the value is `null`, `undefined`, or
102                 // does not define a canonical JSON representation (this applies to
103                 // objects with `toJSON` properties as well, *unless* they are nested
104                 // within an object or array).
105                 stringify(getClass) === undef &&
106                 // IE 8 serializes `undefined` as `"undefined"`. Safari <= 5.1.7 and
107                 // FF 3.1b3 pass this test.
108                 stringify(undef) === undef &&
109                 // Safari <= 5.1.7 and FF 3.1b3 throw `Error`s and `TypeError`s,
110                 // respectively, if the value is omitted entirely.
111                 stringify() === undef &&
112                 // FF 3.1b1, 2 throw an error if the given value is not a number,
113                 // string, array, object, Boolean, or `null` literal. This applies to
114                 // objects with custom `toJSON` methods as well, unless they are nested
115                 // inside object or array literals. YUI 3.0.0b1 ignores custom `toJSON`
116                 // methods entirely.
117                 stringify(value) === "1" &&
118                 stringify([value]) == "[1]" &&
119                 // Prototype <= 1.6.1 serializes `[undefined]` as `"[]"` instead of
120                 // `"[null]"`.
121                 stringify([undef]) == "[null]" &&
122                 // YUI 3.0.0b1 fails to serialize `null` literals.
123                 stringify(null) == "null" &&
124                 // FF 3.1b1, 2 halts serialization if an array contains a function:
125                 // `[1, true, getClass, 1]` serializes as "[1,true,],". FF 3.1b3
126                 // elides non-JSON values from objects and arrays, unless they
127                 // define custom `toJSON` methods.
128                 stringify([undef, getClass, null]) == "[null,null,null]" &&
129                 // Simple serialization test. FF 3.1b1 uses Unicode escape sequences
130                 // where character escape codes are expected (e.g., `\b` => `\u0008`).
131                 stringify({ "a": [value, true, false, null, "\x00\b\n\f\r\t"] }) == serialized &&
132                 // FF 3.1b1 and b2 ignore the `filter` and `width` arguments.
133                 stringify(null, value) === "1" &&
134                 stringify([1, 2], null, 1) == "[\n 1,\n 2\n]" &&
135                 // JSON 2, Prototype <= 1.7, and older WebKit builds incorrectly
136                 // serialize extended years.
137                 stringify(new Date(-8.64e15)) == '"-271821-04-20T00:00:00.000Z"' &&
138                 // The milliseconds are optional in ES 5, but required in 5.1.
139                 stringify(new Date(8.64e15)) == '"+275760-09-13T00:00:00.000Z"' &&
140                 // Firefox <= 11.0 incorrectly serializes years prior to 0 as negative
141                 // four-digit years instead of six-digit years. Credits: @Yaffle.
142                 stringify(new Date(-621987552e5)) == '"-000001-01-01T00:00:00.000Z"' &&
143                 // Safari <= 5.1.5 and Opera >= 10.53 incorrectly serialize millisecond
144                 // values less than 1000. Credits: @Yaffle.
145                 stringify(new Date(-1)) == '"1969-12-31T23:59:59.999Z"';
146             } catch (exception) {
147               stringifySupported = false;
148             }
149           }
150           isSupported = stringifySupported;
151         }
152         // Test `JSON.parse`.
153         if (name == "json-parse") {
154           var parse = exports.parse;
155           if (typeof parse == "function") {
156             try {
157               // FF 3.1b1, b2 will throw an exception if a bare literal is provided.
158               // Conforming implementations should also coerce the initial argument to
159               // a string prior to parsing.
160               if (parse("0") === 0 && !parse(false)) {
161                 // Simple parsing test.
162                 value = parse(serialized);
163                 var parseSupported = value["a"].length == 5 && value["a"][0] === 1;
164                 if (parseSupported) {
165                   try {
166                     // Safari <= 5.1.2 and FF 3.1b1 allow unescaped tabs in strings.
167                     parseSupported = !parse('"\t"');
168                   } catch (exception) {}
169                   if (parseSupported) {
170                     try {
171                       // FF 4.0 and 4.0.1 allow leading `+` signs and leading
172                       // decimal points. FF 4.0, 4.0.1, and IE 9-10 also allow
173                       // certain octal literals.
174                       parseSupported = parse("01") !== 1;
175                     } catch (exception) {}
176                   }
177                   if (parseSupported) {
178                     try {
179                       // FF 4.0, 4.0.1, and Rhino 1.7R3-R4 allow trailing decimal
180                       // points. These environments, along with FF 3.1b1 and 2,
181                       // also allow trailing commas in JSON objects and arrays.
182                       parseSupported = parse("1.") !== 1;
183                     } catch (exception) {}
184                   }
185                 }
186               }
187             } catch (exception) {
188               parseSupported = false;
189             }
190           }
191           isSupported = parseSupported;
192         }
193       }
194       return has[name] = !!isSupported;
195     }
196
197     if (!has("json")) {
198       // Common `[[Class]]` name aliases.
199       var functionClass = "[object Function]",
200           dateClass = "[object Date]",
201           numberClass = "[object Number]",
202           stringClass = "[object String]",
203           arrayClass = "[object Array]",
204           booleanClass = "[object Boolean]";
205
206       // Detect incomplete support for accessing string characters by index.
207       var charIndexBuggy = has("bug-string-char-index");
208
209       // Define additional utility methods if the `Date` methods are buggy.
210       if (!isExtended) {
211         var floor = Math.floor;
212         // A mapping between the months of the year and the number of days between
213         // January 1st and the first of the respective month.
214         var Months = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
215         // Internal: Calculates the number of days between the Unix epoch and the
216         // first day of the given month.
217         var getDay = function (year, month) {
218           return Months[month] + 365 * (year - 1970) + floor((year - 1969 + (month = +(month > 1))) / 4) - floor((year - 1901 + month) / 100) + floor((year - 1601 + month) / 400);
219         };
220       }
221
222       // Internal: Determines if a property is a direct property of the given
223       // object. Delegates to the native `Object#hasOwnProperty` method.
224       if (!(isProperty = objectProto.hasOwnProperty)) {
225         isProperty = function (property) {
226           var members = {}, constructor;
227           if ((members.__proto__ = null, members.__proto__ = {
228             // The *proto* property cannot be set multiple times in recent
229             // versions of Firefox and SeaMonkey.
230             "toString": 1
231           }, members).toString != getClass) {
232             // Safari <= 2.0.3 doesn't implement `Object#hasOwnProperty`, but
233             // supports the mutable *proto* property.
234             isProperty = function (property) {
235               // Capture and break the object's prototype chain (see section 8.6.2
236               // of the ES 5.1 spec). The parenthesized expression prevents an
237               // unsafe transformation by the Closure Compiler.
238               var original = this.__proto__, result = property in (this.__proto__ = null, this);
239               // Restore the original prototype chain.
240               this.__proto__ = original;
241               return result;
242             };
243           } else {
244             // Capture a reference to the top-level `Object` constructor.
245             constructor = members.constructor;
246             // Use the `constructor` property to simulate `Object#hasOwnProperty` in
247             // other environments.
248             isProperty = function (property) {
249               var parent = (this.constructor || constructor).prototype;
250               return property in this && !(property in parent && this[property] === parent[property]);
251             };
252           }
253           members = null;
254           return isProperty.call(this, property);
255         };
256       }
257
258       // Internal: Normalizes the `for...in` iteration algorithm across
259       // environments. Each enumerated key is yielded to a `callback` function.
260       forEach = function (object, callback) {
261         var size = 0, Properties, members, property;
262
263         // Tests for bugs in the current environment's `for...in` algorithm. The
264         // `valueOf` property inherits the non-enumerable flag from
265         // `Object.prototype` in older versions of IE, Netscape, and Mozilla.
266         (Properties = function () {
267           this.valueOf = 0;
268         }).prototype.valueOf = 0;
269
270         // Iterate over a new instance of the `Properties` class.
271         members = new Properties();
272         for (property in members) {
273           // Ignore all properties inherited from `Object.prototype`.
274           if (isProperty.call(members, property)) {
275             size++;
276           }
277         }
278         Properties = members = null;
279
280         // Normalize the iteration algorithm.
281         if (!size) {
282           // A list of non-enumerable properties inherited from `Object.prototype`.
283           members = ["valueOf", "toString", "toLocaleString", "propertyIsEnumerable", "isPrototypeOf", "hasOwnProperty", "constructor"];
284           // IE <= 8, Mozilla 1.0, and Netscape 6.2 ignore shadowed non-enumerable
285           // properties.
286           forEach = function (object, callback) {
287             var isFunction = getClass.call(object) == functionClass, property, length;
288             var hasProperty = !isFunction && typeof object.constructor != "function" && objectTypes[typeof object.hasOwnProperty] && object.hasOwnProperty || isProperty;
289             for (property in object) {
290               // Gecko <= 1.0 enumerates the `prototype` property of functions under
291               // certain conditions; IE does not.
292               if (!(isFunction && property == "prototype") && hasProperty.call(object, property)) {
293                 callback(property);
294               }
295             }
296             // Manually invoke the callback for each non-enumerable property.
297             for (length = members.length; property = members[--length]; hasProperty.call(object, property) && callback(property));
298           };
299         } else if (size == 2) {
300           // Safari <= 2.0.4 enumerates shadowed properties twice.
301           forEach = function (object, callback) {
302             // Create a set of iterated properties.
303             var members = {}, isFunction = getClass.call(object) == functionClass, property;
304             for (property in object) {
305               // Store each property name to prevent double enumeration. The
306               // `prototype` property of functions is not enumerated due to cross-
307               // environment inconsistencies.
308               if (!(isFunction && property == "prototype") && !isProperty.call(members, property) && (members[property] = 1) && isProperty.call(object, property)) {
309                 callback(property);
310               }
311             }
312           };
313         } else {
314           // No bugs detected; use the standard `for...in` algorithm.
315           forEach = function (object, callback) {
316             var isFunction = getClass.call(object) == functionClass, property, isConstructor;
317             for (property in object) {
318               if (!(isFunction && property == "prototype") && isProperty.call(object, property) && !(isConstructor = property === "constructor")) {
319                 callback(property);
320               }
321             }
322             // Manually invoke the callback for the `constructor` property due to
323             // cross-environment inconsistencies.
324             if (isConstructor || isProperty.call(object, (property = "constructor"))) {
325               callback(property);
326             }
327           };
328         }
329         return forEach(object, callback);
330       };
331
332       // Public: Serializes a JavaScript `value` as a JSON string. The optional
333       // `filter` argument may specify either a function that alters how object and
334       // array members are serialized, or an array of strings and numbers that
335       // indicates which properties should be serialized. The optional `width`
336       // argument may be either a string or number that specifies the indentation
337       // level of the output.
338       if (!has("json-stringify")) {
339         // Internal: A map of control characters and their escaped equivalents.
340         var Escapes = {
341           92: "\\\\",
342           34: '\\"',
343           8: "\\b",
344           12: "\\f",
345           10: "\\n",
346           13: "\\r",
347           9: "\\t"
348         };
349
350         // Internal: Converts `value` into a zero-padded string such that its
351         // length is at least equal to `width`. The `width` must be <= 6.
352         var leadingZeroes = "000000";
353         var toPaddedString = function (width, value) {
354           // The `|| 0` expression is necessary to work around a bug in
355           // Opera <= 7.54u2 where `0 == -0`, but `String(-0) !== "0"`.
356           return (leadingZeroes + (value || 0)).slice(-width);
357         };
358
359         // Internal: Double-quotes a string `value`, replacing all ASCII control
360         // characters (characters with code unit values between 0 and 31) with
361         // their escaped equivalents. This is an implementation of the
362         // `Quote(value)` operation defined in ES 5.1 section 15.12.3.
363         var unicodePrefix = "\\u00";
364         var quote = function (value) {
365           var result = '"', index = 0, length = value.length, useCharIndex = !charIndexBuggy || length > 10;
366           var symbols = useCharIndex && (charIndexBuggy ? value.split("") : value);
367           for (; index < length; index++) {
368             var charCode = value.charCodeAt(index);
369             // If the character is a control character, append its Unicode or
370             // shorthand escape sequence; otherwise, append the character as-is.
371             switch (charCode) {
372               case 8: case 9: case 10: case 12: case 13: case 34: case 92:
373                 result += Escapes[charCode];
374                 break;
375               default:
376                 if (charCode < 32) {
377                   result += unicodePrefix + toPaddedString(2, charCode.toString(16));
378                   break;
379                 }
380                 result += useCharIndex ? symbols[index] : value.charAt(index);
381             }
382           }
383           return result + '"';
384         };
385
386         // Internal: Recursively serializes an object. Implements the
387         // `Str(key, holder)`, `JO(value)`, and `JA(value)` operations.
388         var serialize = function (property, object, callback, properties, whitespace, indentation, stack) {
389           var value, className, year, month, date, time, hours, minutes, seconds, milliseconds, results, element, index, length, prefix, result;
390           try {
391             // Necessary for host object support.
392             value = object[property];
393           } catch (exception) {}
394           if (typeof value == "object" && value) {
395             className = getClass.call(value);
396             if (className == dateClass && !isProperty.call(value, "toJSON")) {
397               if (value > -1 / 0 && value < 1 / 0) {
398                 // Dates are serialized according to the `Date#toJSON` method
399                 // specified in ES 5.1 section 15.9.5.44. See section 15.9.1.15
400                 // for the ISO 8601 date time string format.
401                 if (getDay) {
402                   // Manually compute the year, month, date, hours, minutes,
403                   // seconds, and milliseconds if the `getUTC*` methods are
404                   // buggy. Adapted from @Yaffle's `date-shim` project.
405                   date = floor(value / 864e5);
406                   for (year = floor(date / 365.2425) + 1970 - 1; getDay(year + 1, 0) <= date; year++);
407                   for (month = floor((date - getDay(year, 0)) / 30.42); getDay(year, month + 1) <= date; month++);
408                   date = 1 + date - getDay(year, month);
409                   // The `time` value specifies the time within the day (see ES
410                   // 5.1 section 15.9.1.2). The formula `(A % B + B) % B` is used
411                   // to compute `A modulo B`, as the `%` operator does not
412                   // correspond to the `modulo` operation for negative numbers.
413                   time = (value % 864e5 + 864e5) % 864e5;
414                   // The hours, minutes, seconds, and milliseconds are obtained by
415                   // decomposing the time within the day. See section 15.9.1.10.
416                   hours = floor(time / 36e5) % 24;
417                   minutes = floor(time / 6e4) % 60;
418                   seconds = floor(time / 1e3) % 60;
419                   milliseconds = time % 1e3;
420                 } else {
421                   year = value.getUTCFullYear();
422                   month = value.getUTCMonth();
423                   date = value.getUTCDate();
424                   hours = value.getUTCHours();
425                   minutes = value.getUTCMinutes();
426                   seconds = value.getUTCSeconds();
427                   milliseconds = value.getUTCMilliseconds();
428                 }
429                 // Serialize extended years correctly.
430                 value = (year <= 0 || year >= 1e4 ? (year < 0 ? "-" : "+") + toPaddedString(6, year < 0 ? -year : year) : toPaddedString(4, year)) +
431                   "-" + toPaddedString(2, month + 1) + "-" + toPaddedString(2, date) +
432                   // Months, dates, hours, minutes, and seconds should have two
433                   // digits; milliseconds should have three.
434                   "T" + toPaddedString(2, hours) + ":" + toPaddedString(2, minutes) + ":" + toPaddedString(2, seconds) +
435                   // Milliseconds are optional in ES 5.0, but required in 5.1.
436                   "." + toPaddedString(3, milliseconds) + "Z";
437               } else {
438                 value = null;
439               }
440             } else if (typeof value.toJSON == "function" && ((className != numberClass && className != stringClass && className != arrayClass) || isProperty.call(value, "toJSON"))) {
441               // Prototype <= 1.6.1 adds non-standard `toJSON` methods to the
442               // `Number`, `String`, `Date`, and `Array` prototypes. JSON 3
443               // ignores all `toJSON` methods on these objects unless they are
444               // defined directly on an instance.
445               value = value.toJSON(property);
446             }
447           }
448           if (callback) {
449             // If a replacement function was provided, call it to obtain the value
450             // for serialization.
451             value = callback.call(object, property, value);
452           }
453           if (value === null) {
454             return "null";
455           }
456           className = getClass.call(value);
457           if (className == booleanClass) {
458             // Booleans are represented literally.
459             return "" + value;
460           } else if (className == numberClass) {
461             // JSON numbers must be finite. `Infinity` and `NaN` are serialized as
462             // `"null"`.
463             return value > -1 / 0 && value < 1 / 0 ? "" + value : "null";
464           } else if (className == stringClass) {
465             // Strings are double-quoted and escaped.
466             return quote("" + value);
467           }
468           // Recursively serialize objects and arrays.
469           if (typeof value == "object") {
470             // Check for cyclic structures. This is a linear search; performance
471             // is inversely proportional to the number of unique nested objects.
472             for (length = stack.length; length--;) {
473               if (stack[length] === value) {
474                 // Cyclic structures cannot be serialized by `JSON.stringify`.
475                 throw TypeError();
476               }
477             }
478             // Add the object to the stack of traversed objects.
479             stack.push(value);
480             results = [];
481             // Save the current indentation level and indent one additional level.
482             prefix = indentation;
483             indentation += whitespace;
484             if (className == arrayClass) {
485               // Recursively serialize array elements.
486               for (index = 0, length = value.length; index < length; index++) {
487                 element = serialize(index, value, callback, properties, whitespace, indentation, stack);
488                 results.push(element === undef ? "null" : element);
489               }
490               result = results.length ? (whitespace ? "[\n" + indentation + results.join(",\n" + indentation) + "\n" + prefix + "]" : ("[" + results.join(",") + "]")) : "[]";
491             } else {
492               // Recursively serialize object members. Members are selected from
493               // either a user-specified list of property names, or the object
494               // itself.
495               forEach(properties || value, function (property) {
496                 var element = serialize(property, value, callback, properties, whitespace, indentation, stack);
497                 if (element !== undef) {
498                   // According to ES 5.1 section 15.12.3: "If `gap` {whitespace}
499                   // is not the empty string, let `member` {quote(property) + ":"}
500                   // be the concatenation of `member` and the `space` character."
501                   // The "`space` character" refers to the literal space
502                   // character, not the `space` {width} argument provided to
503                   // `JSON.stringify`.
504                   results.push(quote(property) + ":" + (whitespace ? " " : "") + element);
505                 }
506               });
507               result = results.length ? (whitespace ? "{\n" + indentation + results.join(",\n" + indentation) + "\n" + prefix + "}" : ("{" + results.join(",") + "}")) : "{}";
508             }
509             // Remove the object from the traversed object stack.
510             stack.pop();
511             return result;
512           }
513         };
514
515         // Public: `JSON.stringify`. See ES 5.1 section 15.12.3.
516         exports.stringify = function (source, filter, width) {
517           var whitespace, callback, properties, className;
518           if (objectTypes[typeof filter] && filter) {
519             if ((className = getClass.call(filter)) == functionClass) {
520               callback = filter;
521             } else if (className == arrayClass) {
522               // Convert the property names array into a makeshift set.
523               properties = {};
524               for (var index = 0, length = filter.length, value; index < length; value = filter[index++], ((className = getClass.call(value)), className == stringClass || className == numberClass) && (properties[value] = 1));
525             }
526           }
527           if (width) {
528             if ((className = getClass.call(width)) == numberClass) {
529               // Convert the `width` to an integer and create a string containing
530               // `width` number of space characters.
531               if ((width -= width % 1) > 0) {
532                 for (whitespace = "", width > 10 && (width = 10); whitespace.length < width; whitespace += " ");
533               }
534             } else if (className == stringClass) {
535               whitespace = width.length <= 10 ? width : width.slice(0, 10);
536             }
537           }
538           // Opera <= 7.54u2 discards the values associated with empty string keys
539           // (`""`) only if they are used directly within an object member list
540           // (e.g., `!("" in { "": 1})`).
541           return serialize("", (value = {}, value[""] = source, value), callback, properties, whitespace, "", []);
542         };
543       }
544
545       // Public: Parses a JSON source string.
546       if (!has("json-parse")) {
547         var fromCharCode = String.fromCharCode;
548
549         // Internal: A map of escaped control characters and their unescaped
550         // equivalents.
551         var Unescapes = {
552           92: "\\",
553           34: '"',
554           47: "/",
555           98: "\b",
556           116: "\t",
557           110: "\n",
558           102: "\f",
559           114: "\r"
560         };
561
562         // Internal: Stores the parser state.
563         var Index, Source;
564
565         // Internal: Resets the parser state and throws a `SyntaxError`.
566         var abort = function () {
567           Index = Source = null;
568           throw SyntaxError();
569         };
570
571         // Internal: Returns the next token, or `"$"` if the parser has reached
572         // the end of the source string. A token may be a string, number, `null`
573         // literal, or Boolean literal.
574         var lex = function () {
575           var source = Source, length = source.length, value, begin, position, isSigned, charCode;
576           while (Index < length) {
577             charCode = source.charCodeAt(Index);
578             switch (charCode) {
579               case 9: case 10: case 13: case 32:
580                 // Skip whitespace tokens, including tabs, carriage returns, line
581                 // feeds, and space characters.
582                 Index++;
583                 break;
584               case 123: case 125: case 91: case 93: case 58: case 44:
585                 // Parse a punctuator token (`{`, `}`, `[`, `]`, `:`, or `,`) at
586                 // the current position.
587                 value = charIndexBuggy ? source.charAt(Index) : source[Index];
588                 Index++;
589                 return value;
590               case 34:
591                 // `"` delimits a JSON string; advance to the next character and
592                 // begin parsing the string. String tokens are prefixed with the
593                 // sentinel `@` character to distinguish them from punctuators and
594                 // end-of-string tokens.
595                 for (value = "@", Index++; Index < length;) {
596                   charCode = source.charCodeAt(Index);
597                   if (charCode < 32) {
598                     // Unescaped ASCII control characters (those with a code unit
599                     // less than the space character) are not permitted.
600                     abort();
601                   } else if (charCode == 92) {
602                     // A reverse solidus (`\`) marks the beginning of an escaped
603                     // control character (including `"`, `\`, and `/`) or Unicode
604                     // escape sequence.
605                     charCode = source.charCodeAt(++Index);
606                     switch (charCode) {
607                       case 92: case 34: case 47: case 98: case 116: case 110: case 102: case 114:
608                         // Revive escaped control characters.
609                         value += Unescapes[charCode];
610                         Index++;
611                         break;
612                       case 117:
613                         // `\u` marks the beginning of a Unicode escape sequence.
614                         // Advance to the first character and validate the
615                         // four-digit code point.
616                         begin = ++Index;
617                         for (position = Index + 4; Index < position; Index++) {
618                           charCode = source.charCodeAt(Index);
619                           // A valid sequence comprises four hexdigits (case-
620                           // insensitive) that form a single hexadecimal value.
621                           if (!(charCode >= 48 && charCode <= 57 || charCode >= 97 && charCode <= 102 || charCode >= 65 && charCode <= 70)) {
622                             // Invalid Unicode escape sequence.
623                             abort();
624                           }
625                         }
626                         // Revive the escaped character.
627                         value += fromCharCode("0x" + source.slice(begin, Index));
628                         break;
629                       default:
630                         // Invalid escape sequence.
631                         abort();
632                     }
633                   } else {
634                     if (charCode == 34) {
635                       // An unescaped double-quote character marks the end of the
636                       // string.
637                       break;
638                     }
639                     charCode = source.charCodeAt(Index);
640                     begin = Index;
641                     // Optimize for the common case where a string is valid.
642                     while (charCode >= 32 && charCode != 92 && charCode != 34) {
643                       charCode = source.charCodeAt(++Index);
644                     }
645                     // Append the string as-is.
646                     value += source.slice(begin, Index);
647                   }
648                 }
649                 if (source.charCodeAt(Index) == 34) {
650                   // Advance to the next character and return the revived string.
651                   Index++;
652                   return value;
653                 }
654                 // Unterminated string.
655                 abort();
656               default:
657                 // Parse numbers and literals.
658                 begin = Index;
659                 // Advance past the negative sign, if one is specified.
660                 if (charCode == 45) {
661                   isSigned = true;
662                   charCode = source.charCodeAt(++Index);
663                 }
664                 // Parse an integer or floating-point value.
665                 if (charCode >= 48 && charCode <= 57) {
666                   // Leading zeroes are interpreted as octal literals.
667                   if (charCode == 48 && ((charCode = source.charCodeAt(Index + 1)), charCode >= 48 && charCode <= 57)) {
668                     // Illegal octal literal.
669                     abort();
670                   }
671                   isSigned = false;
672                   // Parse the integer component.
673                   for (; Index < length && ((charCode = source.charCodeAt(Index)), charCode >= 48 && charCode <= 57); Index++);
674                   // Floats cannot contain a leading decimal point; however, this
675                   // case is already accounted for by the parser.
676                   if (source.charCodeAt(Index) == 46) {
677                     position = ++Index;
678                     // Parse the decimal component.
679                     for (; position < length && ((charCode = source.charCodeAt(position)), charCode >= 48 && charCode <= 57); position++);
680                     if (position == Index) {
681                       // Illegal trailing decimal.
682                       abort();
683                     }
684                     Index = position;
685                   }
686                   // Parse exponents. The `e` denoting the exponent is
687                   // case-insensitive.
688                   charCode = source.charCodeAt(Index);
689                   if (charCode == 101 || charCode == 69) {
690                     charCode = source.charCodeAt(++Index);
691                     // Skip past the sign following the exponent, if one is
692                     // specified.
693                     if (charCode == 43 || charCode == 45) {
694                       Index++;
695                     }
696                     // Parse the exponential component.
697                     for (position = Index; position < length && ((charCode = source.charCodeAt(position)), charCode >= 48 && charCode <= 57); position++);
698                     if (position == Index) {
699                       // Illegal empty exponent.
700                       abort();
701                     }
702                     Index = position;
703                   }
704                   // Coerce the parsed value to a JavaScript number.
705                   return +source.slice(begin, Index);
706                 }
707                 // A negative sign may only precede numbers.
708                 if (isSigned) {
709                   abort();
710                 }
711                 // `true`, `false`, and `null` literals.
712                 if (source.slice(Index, Index + 4) == "true") {
713                   Index += 4;
714                   return true;
715                 } else if (source.slice(Index, Index + 5) == "false") {
716                   Index += 5;
717                   return false;
718                 } else if (source.slice(Index, Index + 4) == "null") {
719                   Index += 4;
720                   return null;
721                 }
722                 // Unrecognized token.
723                 abort();
724             }
725           }
726           // Return the sentinel `$` character if the parser has reached the end
727           // of the source string.
728           return "$";
729         };
730
731         // Internal: Parses a JSON `value` token.
732         var get = function (value) {
733           var results, hasMembers;
734           if (value == "$") {
735             // Unexpected end of input.
736             abort();
737           }
738           if (typeof value == "string") {
739             if ((charIndexBuggy ? value.charAt(0) : value[0]) == "@") {
740               // Remove the sentinel `@` character.
741               return value.slice(1);
742             }
743             // Parse object and array literals.
744             if (value == "[") {
745               // Parses a JSON array, returning a new JavaScript array.
746               results = [];
747               for (;; hasMembers || (hasMembers = true)) {
748                 value = lex();
749                 // A closing square bracket marks the end of the array literal.
750                 if (value == "]") {
751                   break;
752                 }
753                 // If the array literal contains elements, the current token
754                 // should be a comma separating the previous element from the
755                 // next.
756                 if (hasMembers) {
757                   if (value == ",") {
758                     value = lex();
759                     if (value == "]") {
760                       // Unexpected trailing `,` in array literal.
761                       abort();
762                     }
763                   } else {
764                     // A `,` must separate each array element.
765                     abort();
766                   }
767                 }
768                 // Elisions and leading commas are not permitted.
769                 if (value == ",") {
770                   abort();
771                 }
772                 results.push(get(value));
773               }
774               return results;
775             } else if (value == "{") {
776               // Parses a JSON object, returning a new JavaScript object.
777               results = {};
778               for (;; hasMembers || (hasMembers = true)) {
779                 value = lex();
780                 // A closing curly brace marks the end of the object literal.
781                 if (value == "}") {
782                   break;
783                 }
784                 // If the object literal contains members, the current token
785                 // should be a comma separator.
786                 if (hasMembers) {
787                   if (value == ",") {
788                     value = lex();
789                     if (value == "}") {
790                       // Unexpected trailing `,` in object literal.
791                       abort();
792                     }
793                   } else {
794                     // A `,` must separate each object member.
795                     abort();
796                   }
797                 }
798                 // Leading commas are not permitted, object property names must be
799                 // double-quoted strings, and a `:` must separate each property
800                 // name and value.
801                 if (value == "," || typeof value != "string" || (charIndexBuggy ? value.charAt(0) : value[0]) != "@" || lex() != ":") {
802                   abort();
803                 }
804                 results[value.slice(1)] = get(lex());
805               }
806               return results;
807             }
808             // Unexpected token encountered.
809             abort();
810           }
811           return value;
812         };
813
814         // Internal: Updates a traversed object member.
815         var update = function (source, property, callback) {
816           var element = walk(source, property, callback);
817           if (element === undef) {
818             delete source[property];
819           } else {
820             source[property] = element;
821           }
822         };
823
824         // Internal: Recursively traverses a parsed JSON object, invoking the
825         // `callback` function for each value. This is an implementation of the
826         // `Walk(holder, name)` operation defined in ES 5.1 section 15.12.2.
827         var walk = function (source, property, callback) {
828           var value = source[property], length;
829           if (typeof value == "object" && value) {
830             // `forEach` can't be used to traverse an array in Opera <= 8.54
831             // because its `Object#hasOwnProperty` implementation returns `false`
832             // for array indices (e.g., `![1, 2, 3].hasOwnProperty("0")`).
833             if (getClass.call(value) == arrayClass) {
834               for (length = value.length; length--;) {
835                 update(value, length, callback);
836               }
837             } else {
838               forEach(value, function (property) {
839                 update(value, property, callback);
840               });
841             }
842           }
843           return callback.call(source, property, value);
844         };
845
846         // Public: `JSON.parse`. See ES 5.1 section 15.12.2.
847         exports.parse = function (source, callback) {
848           var result, value;
849           Index = 0;
850           Source = "" + source;
851           result = get(lex());
852           // If a JSON string contains multiple tokens, it is invalid.
853           if (lex() != "$") {
854             abort();
855           }
856           // Reset the parser state.
857           Index = Source = null;
858           return callback && getClass.call(callback) == functionClass ? walk((value = {}, value[""] = result, value), "", callback) : result;
859         };
860       }
861     }
862
863     exports["runInContext"] = runInContext;
864     return exports;
865   }
866
867   if (freeExports && !isLoader) {
868     // Export for CommonJS environments.
869     runInContext(root, freeExports);
870   } else {
871     // Export for web browsers and JavaScript engines.
872     var nativeJSON = root.JSON,
873         previousJSON = root["JSON3"],
874         isRestored = false;
875
876     var JSON3 = runInContext(root, (root["JSON3"] = {
877       // Public: Restores the original value of the global `JSON` object and
878       // returns a reference to the `JSON3` object.
879       "noConflict": function () {
880         if (!isRestored) {
881           isRestored = true;
882           root.JSON = nativeJSON;
883           root["JSON3"] = previousJSON;
884           nativeJSON = previousJSON = null;
885         }
886         return JSON3;
887       }
888     }));
889
890     root.JSON = {
891       "parse": JSON3.parse,
892       "stringify": JSON3.stringify
893     };
894   }
895
896   // Export for asynchronous module loaders.
897   if (isLoader) {
898     define(function () {
899       return JSON3;
900     });
901   }
902 }).call(this);