JSON.html 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-JSON'>/**
  19. </span> * Modified version of [Douglas Crockford's JSON.js][dc] that doesn't
  20. * mess with the Object prototype.
  21. *
  22. * [dc]: http://www.json.org/js.html
  23. *
  24. * @singleton
  25. */
  26. Ext.JSON = (new(function() {
  27. var me = this,
  28. encodingFunction,
  29. decodingFunction,
  30. useNative = null,
  31. useHasOwn = !! {}.hasOwnProperty,
  32. isNative = function() {
  33. if (useNative === null) {
  34. useNative = Ext.USE_NATIVE_JSON &amp;&amp; window.JSON &amp;&amp; JSON.toString() == '[object JSON]';
  35. }
  36. return useNative;
  37. },
  38. pad = function(n) {
  39. return n &lt; 10 ? &quot;0&quot; + n : n;
  40. },
  41. doDecode = function(json) {
  42. return eval(&quot;(&quot; + json + ')');
  43. },
  44. doEncode = function(o, newline) {
  45. // http://jsperf.com/is-undefined
  46. if (o === null || o === undefined) {
  47. return &quot;null&quot;;
  48. } else if (Ext.isDate(o)) {
  49. return Ext.JSON.encodeDate(o);
  50. } else if (Ext.isString(o)) {
  51. return Ext.JSON.encodeString(o);
  52. } else if (typeof o == &quot;number&quot;) {
  53. //don't use isNumber here, since finite checks happen inside isNumber
  54. return isFinite(o) ? String(o) : &quot;null&quot;;
  55. } else if (Ext.isBoolean(o)) {
  56. return String(o);
  57. }
  58. // Allow custom zerialization by adding a toJSON method to any object type.
  59. // Date/String have a toJSON in some environments, so check these first.
  60. else if (o.toJSON) {
  61. return o.toJSON();
  62. } else if (Ext.isArray(o)) {
  63. return encodeArray(o, newline);
  64. } else if (Ext.isObject(o)) {
  65. return encodeObject(o, newline);
  66. } else if (typeof o === &quot;function&quot;) {
  67. return &quot;null&quot;;
  68. }
  69. return 'undefined';
  70. },
  71. m = {
  72. &quot;\b&quot;: '\\b',
  73. &quot;\t&quot;: '\\t',
  74. &quot;\n&quot;: '\\n',
  75. &quot;\f&quot;: '\\f',
  76. &quot;\r&quot;: '\\r',
  77. '&quot;': '\\&quot;',
  78. &quot;\\&quot;: '\\\\',
  79. '\x0b': '\\u000b' //ie doesn't handle \v
  80. },
  81. charToReplace = /[\\\&quot;\x00-\x1f\x7f-\uffff]/g,
  82. encodeString = function(s) {
  83. return '&quot;' + s.replace(charToReplace, function(a) {
  84. var c = m[a];
  85. return typeof c === 'string' ? c : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  86. }) + '&quot;';
  87. },
  88. //&lt;debug&gt;
  89. encodeArrayPretty = function(o, newline) {
  90. var len = o.length,
  91. cnewline = newline + ' ',
  92. sep = ',' + cnewline,
  93. a = [&quot;[&quot;, cnewline], // Note newline in case there are no members
  94. i;
  95. for (i = 0; i &lt; len; i += 1) {
  96. a.push(Ext.JSON.encodeValue(o[i], cnewline), sep);
  97. }
  98. // Overwrite trailing comma (or empty string)
  99. a[a.length - 1] = newline + ']';
  100. return a.join('');
  101. },
  102. encodeObjectPretty = function(o, newline) {
  103. var cnewline = newline + ' ',
  104. sep = ',' + cnewline,
  105. a = [&quot;{&quot;, cnewline], // Note newline in case there are no members
  106. i;
  107. for (i in o) {
  108. if (!useHasOwn || o.hasOwnProperty(i)) {
  109. a.push(Ext.JSON.encodeValue(i) + ': ' + Ext.JSON.encodeValue(o[i], cnewline), sep);
  110. }
  111. }
  112. // Overwrite trailing comma (or empty string)
  113. a[a.length - 1] = newline + '}';
  114. return a.join('');
  115. },
  116. //&lt;/debug&gt;
  117. encodeArray = function(o, newline) {
  118. //&lt;debug&gt;
  119. if (newline) {
  120. return encodeArrayPretty(o, newline);
  121. }
  122. //&lt;/debug&gt;
  123. var a = [&quot;[&quot;, &quot;&quot;], // Note empty string in case there are no serializable members.
  124. len = o.length,
  125. i;
  126. for (i = 0; i &lt; len; i += 1) {
  127. a.push(Ext.JSON.encodeValue(o[i]), ',');
  128. }
  129. // Overwrite trailing comma (or empty string)
  130. a[a.length - 1] = ']';
  131. return a.join(&quot;&quot;);
  132. },
  133. encodeObject = function(o, newline) {
  134. //&lt;debug&gt;
  135. if (newline) {
  136. return encodeObjectPretty(o, newline);
  137. }
  138. //&lt;/debug&gt;
  139. var a = [&quot;{&quot;, &quot;&quot;], // Note empty string in case there are no serializable members.
  140. i;
  141. for (i in o) {
  142. if (!useHasOwn || o.hasOwnProperty(i)) {
  143. a.push(Ext.JSON.encodeValue(i), &quot;:&quot;, Ext.JSON.encodeValue(o[i]), ',');
  144. }
  145. }
  146. // Overwrite trailing comma (or empty string)
  147. a[a.length - 1] = '}';
  148. return a.join(&quot;&quot;);
  149. };
  150. <span id='Ext-JSON-method-encodeString'> /**
  151. </span> * Encodes a String. This returns the actual string which is inserted into the JSON string as the literal
  152. * expression. **The returned value includes enclosing double quotation marks.**
  153. *
  154. * To override this:
  155. *
  156. * Ext.JSON.encodeString = function(s) {
  157. * return 'Foo' + s;
  158. * };
  159. *
  160. * @param {String} s The String to encode
  161. * @return {String} The string literal to use in a JSON string.
  162. * @method
  163. */
  164. me.encodeString = encodeString;
  165. <span id='Ext-JSON-method-encodeValue'> /**
  166. </span> * The function which {@link #encode} uses to encode all javascript values to their JSON representations
  167. * when {@link Ext#USE_NATIVE_JSON} is `false`.
  168. *
  169. * This is made public so that it can be replaced with a custom implementation.
  170. *
  171. * @param {Object} o Any javascript value to be converted to its JSON representation
  172. * @return {String} The JSON representation of the passed value.
  173. * @method
  174. */
  175. me.encodeValue = doEncode;
  176. <span id='Ext-JSON-method-encodeDate'> /**
  177. </span> * Encodes a Date. This returns the actual string which is inserted into the JSON string as the literal
  178. * expression. **The returned value includes enclosing double quotation marks.**
  179. *
  180. * The default return format is `&quot;yyyy-mm-ddThh:mm:ss&quot;`.
  181. *
  182. * To override this:
  183. *
  184. * Ext.JSON.encodeDate = function(d) {
  185. * return Ext.Date.format(d, '&quot;Y-m-d&quot;');
  186. * };
  187. *
  188. * @param {Date} d The Date to encode
  189. * @return {String} The string literal to use in a JSON string.
  190. */
  191. me.encodeDate = function(o) {
  192. return '&quot;' + o.getFullYear() + &quot;-&quot;
  193. + pad(o.getMonth() + 1) + &quot;-&quot;
  194. + pad(o.getDate()) + &quot;T&quot;
  195. + pad(o.getHours()) + &quot;:&quot;
  196. + pad(o.getMinutes()) + &quot;:&quot;
  197. + pad(o.getSeconds()) + '&quot;';
  198. };
  199. <span id='Ext-JSON-method-encode'> /**
  200. </span> * Encodes an Object, Array or other value.
  201. *
  202. * If the environment's native JSON encoding is not being used ({@link Ext#USE_NATIVE_JSON} is not set,
  203. * or the environment does not support it), then ExtJS's encoding will be used. This allows the developer
  204. * to add a `toJSON` method to their classes which need serializing to return a valid JSON representation
  205. * of the object.
  206. *
  207. * @param {Object} o The variable to encode
  208. * @return {String} The JSON string
  209. */
  210. me.encode = function(o) {
  211. if (!encodingFunction) {
  212. // setup encoding function on first access
  213. encodingFunction = isNative() ? JSON.stringify : me.encodeValue;
  214. }
  215. return encodingFunction(o);
  216. };
  217. <span id='Ext-JSON-method-decode'> /**
  218. </span> * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws
  219. * a SyntaxError unless the safe option is set.
  220. *
  221. * @param {String} json The JSON string
  222. * @param {Boolean} [safe=false] True to return null, false to throw an exception if the JSON is invalid.
  223. * @return {Object} The resulting object
  224. */
  225. me.decode = function(json, safe) {
  226. if (!decodingFunction) {
  227. // setup decoding function on first access
  228. decodingFunction = isNative() ? JSON.parse : doDecode;
  229. }
  230. try {
  231. return decodingFunction(json);
  232. } catch (e) {
  233. if (safe === true) {
  234. return null;
  235. }
  236. Ext.Error.raise({
  237. sourceClass: &quot;Ext.JSON&quot;,
  238. sourceMethod: &quot;decode&quot;,
  239. msg: &quot;You're trying to decode an invalid JSON String: &quot; + json
  240. });
  241. }
  242. };
  243. })());
  244. <span id='Ext-method-encode'>/**
  245. </span> * Shorthand for {@link Ext.JSON#encode}
  246. * @member Ext
  247. * @method encode
  248. * @inheritdoc Ext.JSON#encode
  249. */
  250. Ext.encode = Ext.JSON.encode;
  251. <span id='Ext-method-decode'>/**
  252. </span> * Shorthand for {@link Ext.JSON#decode}
  253. * @member Ext
  254. * @method decode
  255. * @inheritdoc Ext.JSON#decode
  256. */
  257. Ext.decode = Ext.JSON.decode;</pre>
  258. </body>
  259. </html>