A unf. social network done poorly.
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.

228 lines
6.9KB

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