/source/Plug-in/jq/json.js

http://prosporous.googlecode.com/ · JavaScript · 226 lines · 181 code · 41 blank · 4 comment · 37 complexity · abd32e4a8bbfc47153c50b62aba22984 MD5 · raw file

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