You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

938 lines
42 KiB

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