json3.js 39 KB

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