json2.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. var JSON;
  2. if (!JSON) {
  3. JSON = {};
  4. }
  5. (function () {
  6. 'use strict';
  7. function f(n) {
  8. // Format integers to have at least two digits.
  9. return n < 10 ? '0' + n : n;
  10. }
  11. if (typeof Date.prototype.toJSON !== 'function') {
  12. Date.prototype.toJSON = function (key) {
  13. return isFinite(this.valueOf())
  14. ? this.getUTCFullYear() + '-' +
  15. f(this.getUTCMonth() + 1) + '-' +
  16. f(this.getUTCDate()) + 'T' +
  17. f(this.getUTCHours()) + ':' +
  18. f(this.getUTCMinutes()) + ':' +
  19. f(this.getUTCSeconds()) + 'Z'
  20. : null;
  21. };
  22. String.prototype.toJSON =
  23. Number.prototype.toJSON =
  24. Boolean.prototype.toJSON = function (key) {
  25. return this.valueOf();
  26. };
  27. }
  28. var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
  29. escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
  30. gap,
  31. indent,
  32. meta = { // table of character substitutions
  33. '\b': '\\b',
  34. '\t': '\\t',
  35. '\n': '\\n',
  36. '\f': '\\f',
  37. '\r': '\\r',
  38. '"' : '\\"',
  39. '\\': '\\\\'
  40. },
  41. rep;
  42. function quote(string) {
  43. escapable.lastIndex = 0;
  44. return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
  45. var c = meta[a];
  46. return typeof c === 'string'
  47. ? c
  48. : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  49. }) + '"' : '"' + string + '"';
  50. }
  51. function str(key, holder) {
  52. var i, // The loop counter.
  53. k, // The member key.
  54. v, // The member value.
  55. length,
  56. mind = gap,
  57. partial,
  58. value = holder[key];
  59. if (value && typeof value === 'object' &&
  60. typeof value.toJSON === 'function') {
  61. value = value.toJSON(key);
  62. }
  63. if (typeof rep === 'function') {
  64. value = rep.call(holder, key, value);
  65. }
  66. switch (typeof value) {
  67. case 'string':
  68. return quote(value);
  69. case 'number':
  70. return isFinite(value) ? String(value) : 'null';
  71. case 'boolean':
  72. case 'null':
  73. return String(value);
  74. case 'object':
  75. if (!value) {
  76. return 'null';
  77. }
  78. gap += indent;
  79. partial = [];
  80. if (Object.prototype.toString.apply(value) === '[object Array]') {
  81. length = value.length;
  82. for (i = 0; i < length; i += 1) {
  83. partial[i] = str(i, value) || 'null';
  84. }
  85. v = partial.length === 0
  86. ? '[]'
  87. : gap
  88. ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
  89. : '[' + partial.join(',') + ']';
  90. gap = mind;
  91. return v;
  92. }
  93. if (rep && typeof rep === 'object') {
  94. length = rep.length;
  95. for (i = 0; i < length; i += 1) {
  96. if (typeof rep[i] === 'string') {
  97. k = rep[i];
  98. v = str(k, value);
  99. if (v) {
  100. partial.push(quote(k) + (gap ? ': ' : ':') + v);
  101. }
  102. }
  103. }
  104. } else {
  105. for (k in value) {
  106. if (Object.prototype.hasOwnProperty.call(value, k)) {
  107. v = str(k, value);
  108. if (v) {
  109. partial.push(quote(k) + (gap ? ': ' : ':') + v);
  110. }
  111. }
  112. }
  113. }
  114. v = partial.length === 0
  115. ? '{}'
  116. : gap
  117. ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
  118. : '{' + partial.join(',') + '}';
  119. gap = mind;
  120. return v;
  121. }
  122. }
  123. if (typeof JSON.stringify !== 'function') {
  124. JSON.stringify = function (value, replacer, space) {
  125. var i;
  126. gap = '';
  127. indent = '';
  128. if (typeof space === 'number') {
  129. for (i = 0; i < space; i += 1) {
  130. indent += ' ';
  131. }
  132. } else if (typeof space === 'string') {
  133. indent = space;
  134. }
  135. rep = replacer;
  136. if (replacer && typeof replacer !== 'function' &&
  137. (typeof replacer !== 'object' ||
  138. typeof replacer.length !== 'number')) {
  139. throw new Error('JSON.stringify');
  140. }
  141. return str('', {'': value});
  142. };
  143. }
  144. if (typeof JSON.parse !== 'function') {
  145. JSON.parse = function (text, reviver) {
  146. var j;
  147. function walk(holder, key) {
  148. var k, v, value = holder[key];
  149. if (value && typeof value === 'object') {
  150. for (k in value) {
  151. if (Object.prototype.hasOwnProperty.call(value, k)) {
  152. v = walk(value, k);
  153. if (v !== undefined) {
  154. value[k] = v;
  155. } else {
  156. delete value[k];
  157. }
  158. }
  159. }
  160. }
  161. return reviver.call(holder, key, value);
  162. }
  163. text = String(text);
  164. cx.lastIndex = 0;
  165. if (cx.test(text)) {
  166. text = text.replace(cx, function (a) {
  167. return '\\u' +
  168. ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  169. });
  170. }
  171. if (/^[\],:{}\s]*$/
  172. .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
  173. .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
  174. .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
  175. j = eval('(' + text + ')');
  176. return typeof reviver === 'function'
  177. ? walk({'': j}, '')
  178. : j;
  179. }
  180. throw new SyntaxError('JSON.parse');
  181. };
  182. }
  183. }());