json.js 6.2 KB

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