PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/scripts/json.js

http://showslow.googlecode.com/
JavaScript | 129 lines | 100 code | 5 blank | 24 comment | 5 complexity | 7f5bd3537a3e95fc8e102989c664fdb6 MD5 | raw file
  1. /*
  2. * Copied directly from http://www.json.org/json.js.
  3. */
  4. /*
  5. json.js
  6. 2006-04-28
  7. This file adds these methods to JavaScript:
  8. object.toJSONString()
  9. This method produces a JSON text from an object. The
  10. object must not contain any cyclical references.
  11. array.toJSONString()
  12. This method produces a JSON text from an array. The
  13. array must not contain any cyclical references.
  14. string.parseJSON()
  15. This method parses a JSON text to produce an object or
  16. array. It will return false if there is an error.
  17. */
  18. SimileAjax.JSON = new Object();
  19. (function () {
  20. var m = {
  21. '\b': '\\b',
  22. '\t': '\\t',
  23. '\n': '\\n',
  24. '\f': '\\f',
  25. '\r': '\\r',
  26. '"' : '\\"',
  27. '\\': '\\\\'
  28. };
  29. var s = {
  30. array: function (x) {
  31. var a = ['['], b, f, i, l = x.length, v;
  32. for (i = 0; i < l; i += 1) {
  33. v = x[i];
  34. f = s[typeof v];
  35. if (f) {
  36. v = f(v);
  37. if (typeof v == 'string') {
  38. if (b) {
  39. a[a.length] = ',';
  40. }
  41. a[a.length] = v;
  42. b = true;
  43. }
  44. }
  45. }
  46. a[a.length] = ']';
  47. return a.join('');
  48. },
  49. 'boolean': function (x) {
  50. return String(x);
  51. },
  52. 'null': function (x) {
  53. return "null";
  54. },
  55. number: function (x) {
  56. return isFinite(x) ? String(x) : 'null';
  57. },
  58. object: function (x) {
  59. if (x) {
  60. if (x instanceof Array) {
  61. return s.array(x);
  62. }
  63. var a = ['{'], b, f, i, v;
  64. for (i in x) {
  65. v = x[i];
  66. f = s[typeof v];
  67. if (f) {
  68. v = f(v);
  69. if (typeof v == 'string') {
  70. if (b) {
  71. a[a.length] = ',';
  72. }
  73. a.push(s.string(i), ':', v);
  74. b = true;
  75. }
  76. }
  77. }
  78. a[a.length] = '}';
  79. return a.join('');
  80. }
  81. return 'null';
  82. },
  83. string: function (x) {
  84. if (/["\\\x00-\x1f]/.test(x)) {
  85. x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
  86. var c = m[b];
  87. if (c) {
  88. return c;
  89. }
  90. c = b.charCodeAt();
  91. return '\\u00' +
  92. Math.floor(c / 16).toString(16) +
  93. (c % 16).toString(16);
  94. });
  95. }
  96. return '"' + x + '"';
  97. }
  98. };
  99. SimileAjax.JSON.toJSONString = function(o) {
  100. if (o instanceof Object) {
  101. return s.object(o);
  102. } else if (o instanceof Array) {
  103. return s.array(o);
  104. } else {
  105. return o.toString();
  106. }
  107. };
  108. SimileAjax.JSON.parseJSON = function () {
  109. try {
  110. return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
  111. this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
  112. eval('(' + this + ')');
  113. } catch (e) {
  114. return false;
  115. }
  116. };
  117. })();