Posted By

roberocity on 10/10/06


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

vali29


JSON


 / Published in: JavaScript
 

probably outdated

  1. /*
  2. Copyright (c) 2005 JSON.org
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10.  
  11. The Software shall be used for Good, not Evil.
  12.  
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21.  
  22. /*
  23.   The global object JSON contains two methods.
  24.  
  25.   JSON.stringify(value) takes a JavaScript value and produces a JSON text.
  26.   The value must not be cyclical.
  27.  
  28.   JSON.parse(text) takes a JSON text and produces a JavaScript value. It will
  29.   return false if there is an error.
  30. */
  31. var JSON = function () {
  32. var m = {
  33. '\b': '\\b',
  34. '\t': '\\t',
  35. '\n': '\\n',
  36. '\f': '\\f',
  37. '\r': '\\r',
  38. '"' : '\\"',
  39. '\\': '\\\\'
  40. },
  41. s = {
  42. 'boolean': function (x) {
  43. return String(x);
  44. },
  45. number: function (x) {
  46. return isFinite(x) ? String(x) : 'null';
  47. },
  48. string: function (x) {
  49. if (/["\\\x00-\x1f]/.test(x)) {
  50. x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
  51. var c = m[b];
  52. if (c) {
  53. return c;
  54. }
  55. c = b.charCodeAt();
  56. return '\\u00' +
  57. Math.floor(c / 16).toString(16) +
  58. (c % 16).toString(16);
  59. });
  60. }
  61. return '"' + x + '"';
  62. },
  63. object: function (x) {
  64. if (x) {
  65. var a = [], b, f, i, l, v;
  66. if (x instanceof Array) {
  67. a[0] = '[';
  68. l = x.length;
  69. for (i = 0; i < l; i += 1) {
  70. v = x[i];
  71. f = s[typeof v];
  72. if (f) {
  73. v = f(v);
  74. if (typeof v == 'string') {
  75. if (b) {
  76. a[a.length] = ',';
  77. }
  78. a[a.length] = v;
  79. b = true;
  80. }
  81. }
  82. }
  83. a[a.length] = ']';
  84. } else if (x instanceof Object) {
  85. a[0] = '{';
  86. for (i in x) {
  87. v = x[i];
  88. f = s[typeof v];
  89. if (f) {
  90. v = f(v);
  91. if (typeof v == 'string') {
  92. if (b) {
  93. a[a.length] = ',';
  94. }
  95. a.push(s.string(i), ':', v);
  96. b = true;
  97. }
  98. }
  99. }
  100. a[a.length] = '}';
  101. } else {
  102. return;
  103. }
  104. return a.join('');
  105. }
  106. return 'null';
  107. }
  108. };
  109. return {
  110. copyright: '(c)2005 JSON.org',
  111. license: 'http://www.crockford.com/JSON/license.html',
  112. /*
  113.   Stringify a JavaScript value, producing a JSON text.
  114. */
  115. stringify: function (v) {
  116. var f = s[typeof v];
  117. if (f) {
  118. v = f(v);
  119. if (typeof v == 'string') {
  120. return v;
  121. }
  122. }
  123. return null;
  124. },
  125. /*
  126.   Parse a JSON text, producing a JavaScript value.
  127.   It returns false if there is a syntax error.
  128. */
  129. parse: function (text) {
  130. try {
  131. return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
  132. text.replace(/"(\\.|[^"\\])*"/g, ''))) &&
  133. eval('(' + text + ')');
  134. } catch (e) {
  135. return false;
  136. }
  137. }
  138. };
  139. }();

Report this snippet  

You need to login to post a comment.