json.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. Copyright (c) 2005 JSON.org
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The Software shall be used for Good, not Evil.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  15. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  16. SOFTWARE.
  17. */
  18. /*
  19. The global object JSON contains two methods.
  20. JSON.stringify(value) takes a JavaScript value and produces a JSON text.
  21. The value must not be cyclical.
  22. JSON.parse(text) takes a JSON text and produces a JavaScript value. It will
  23. throw a 'JSONError' exception if there is an error.
  24. */
  25. if (typeof window.RadControlsNamespace == "undefined")
  26. {
  27. window.RadControlsNamespace = {};
  28. }
  29. if (
  30. typeof(window.RadControlsNamespace.JSON) == "undefined" ||
  31. typeof(window.RadControlsNamespace.JSON.Version) == null ||
  32. window.RadControlsNamespace.JSON.Version < 1
  33. )
  34. {
  35. window.RadControlsNamespace.JSON = {
  36. Version : 1, // Change the version when make changes. Change the value in the IF also
  37. copyright: '(c)2005 JSON.org',
  38. license: 'http://www.crockford.com/JSON/license.html',
  39. /*
  40. Stringify a JavaScript value, producing a JSON text.
  41. */
  42. stringify: function (v, def) {
  43. var a = [];
  44. var deep = arguments[2] || {};
  45. /*
  46. Emit a string.
  47. */
  48. function e(s) {
  49. a[a.length] = s;
  50. }
  51. /*
  52. Convert a value.
  53. */
  54. function g(x)
  55. {
  56. var c, i, l, v;
  57. switch (typeof x) {
  58. case 'object':
  59. if (x) {
  60. if (x instanceof Array) {
  61. e('[');
  62. l = a.length;
  63. for (i = 0; i < x.length; i += 1) {
  64. v = x[i];
  65. if (typeof v != 'undefined' &&
  66. typeof v != 'function') {
  67. if (l < a.length) {
  68. e(',');
  69. }
  70. g(v);
  71. }
  72. }
  73. e(']');
  74. return '';
  75. } else if (typeof x.valueOf == 'function')
  76. {
  77. e('{');
  78. l = a.length;
  79. for (i in x)
  80. {
  81. v = x[i];
  82. // Modified to skip default props
  83. if (def && v == def[i])
  84. {
  85. continue;
  86. }
  87. var type = typeof v;
  88. if (
  89. type == 'undefined' ||
  90. type == 'function'
  91. )
  92. {
  93. continue;
  94. }
  95. if (type == "object" && !deep[i])
  96. {
  97. continue;
  98. }
  99. if (l < a.length)
  100. {
  101. e(',');
  102. }
  103. g(i);
  104. e(':');
  105. g(v);
  106. }
  107. return e('}');
  108. }
  109. }
  110. e('null');
  111. return '';
  112. case 'number':
  113. e(isFinite(x) ? +x : 'null');
  114. return '';
  115. case 'string':
  116. l = x.length;
  117. e('"');
  118. for (i = 0; i < l; i += 1) {
  119. c = x.charAt(i);
  120. if (c >= ' ') {
  121. if (c == '\\' || c == '"') {
  122. e('\\');
  123. }
  124. e(c);
  125. } else {
  126. switch (c) {
  127. case '\b':
  128. e('\\b');
  129. break;
  130. case '\f':
  131. e('\\f');
  132. break;
  133. case '\n':
  134. e('\\n');
  135. break;
  136. case '\r':
  137. e('\\r');
  138. break;
  139. case '\t':
  140. e('\\t');
  141. break;
  142. default:
  143. c = c.charCodeAt();
  144. e('\\u00' + Math.floor(c / 16).toString(16) +
  145. (c % 16).toString(16));
  146. }
  147. }
  148. }
  149. e('"');
  150. return '';
  151. case 'boolean':
  152. e(String(x));
  153. return '';
  154. default:
  155. e('null');
  156. return '';
  157. }
  158. }
  159. g(v, 0);
  160. return a.join('');
  161. },
  162. // Addition
  163. stringifyHashTable : function (hash, idMember, initial)
  164. {
  165. var a = [];
  166. if (!initial) initial = [];
  167. for (var i = 0; i < hash.length; i ++)
  168. {
  169. var ser = this.stringify(hash[i], initial[i]);
  170. if (ser == '{}') continue;
  171. a[a.length] = "\"" + hash[i][idMember] + "\":" + ser;
  172. }
  173. return "{" + a.join(',') + "}";
  174. },
  175. /*
  176. Parse a JSON text, producing a JavaScript value.
  177. */
  178. parse: function (text) {
  179. return (/^([ \t\r\n,:{}\[\]]|"(\\["\\\/bfnrtu]|[^\x00-\x1f"\\]+)*"|-?\d+(\.\d*)?([eE][+-]?\d+)?|true|false|null)+$/.test(text)) &&
  180. eval('(' + text + ')');
  181. }
  182. }
  183. }
  184. //BEGIN_ATLAS_NOTIFY
  185. if (typeof(Sys) != "undefined")
  186. {
  187. if (Sys.Application != null && Sys.Application.notifyScriptLoaded != null)
  188. {
  189. Sys.Application.notifyScriptLoaded();
  190. }
  191. }
  192. //END_ATLAS_NOTIFY