You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
7.5 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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-util-JSON'>/**
  19. </span> * @class Ext.util.JSON
  20. * Modified version of Douglas Crockford&quot;s json.js that doesn&quot;t
  21. * mess with the Object prototype
  22. * http://www.json.org/js.html
  23. * @singleton
  24. */
  25. Ext.util.JSON = new (function(){
  26. var useHasOwn = !!{}.hasOwnProperty,
  27. isNative = function() {
  28. var useNative = null;
  29. return function() {
  30. if (useNative === null) {
  31. useNative = Ext.USE_NATIVE_JSON &amp;&amp; window.JSON &amp;&amp; JSON.toString() == '[object JSON]';
  32. }
  33. return useNative;
  34. };
  35. }(),
  36. pad = function(n) {
  37. return n &lt; 10 ? &quot;0&quot; + n : n;
  38. },
  39. doDecode = function(json){
  40. return json ? eval(&quot;(&quot; + json + &quot;)&quot;) : &quot;&quot;;
  41. },
  42. doEncode = function(o){
  43. if(!Ext.isDefined(o) || o === null){
  44. return &quot;null&quot;;
  45. }else if(Ext.isArray(o)){
  46. return encodeArray(o);
  47. }else if(Ext.isDate(o)){
  48. return Ext.util.JSON.encodeDate(o);
  49. }else if(Ext.isString(o)){
  50. return encodeString(o);
  51. }else if(typeof o == &quot;number&quot;){
  52. //don't use isNumber here, since finite checks happen inside isNumber
  53. return isFinite(o) ? String(o) : &quot;null&quot;;
  54. }else if(Ext.isBoolean(o)){
  55. return String(o);
  56. }else {
  57. var a = [&quot;{&quot;], b, i, v;
  58. for (i in o) {
  59. // don't encode DOM objects
  60. if(!o.getElementsByTagName){
  61. if(!useHasOwn || o.hasOwnProperty(i)) {
  62. v = o[i];
  63. switch (typeof v) {
  64. case &quot;undefined&quot;:
  65. case &quot;function&quot;:
  66. case &quot;unknown&quot;:
  67. break;
  68. default:
  69. if(b){
  70. a.push(',');
  71. }
  72. a.push(doEncode(i), &quot;:&quot;,
  73. v === null ? &quot;null&quot; : doEncode(v));
  74. b = true;
  75. }
  76. }
  77. }
  78. }
  79. a.push(&quot;}&quot;);
  80. return a.join(&quot;&quot;);
  81. }
  82. },
  83. m = {
  84. &quot;\b&quot;: '\\b',
  85. &quot;\t&quot;: '\\t',
  86. &quot;\n&quot;: '\\n',
  87. &quot;\f&quot;: '\\f',
  88. &quot;\r&quot;: '\\r',
  89. '&quot;' : '\\&quot;',
  90. &quot;\\&quot;: '\\\\'
  91. },
  92. encodeString = function(s){
  93. if (/[&quot;\\\x00-\x1f]/.test(s)) {
  94. return '&quot;' + s.replace(/([\x00-\x1f\\&quot;])/g, function(a, b) {
  95. var c = m[b];
  96. if(c){
  97. return c;
  98. }
  99. c = b.charCodeAt();
  100. return &quot;\\u00&quot; +
  101. Math.floor(c / 16).toString(16) +
  102. (c % 16).toString(16);
  103. }) + '&quot;';
  104. }
  105. return '&quot;' + s + '&quot;';
  106. },
  107. encodeArray = function(o){
  108. var a = [&quot;[&quot;], b, i, l = o.length, v;
  109. for (i = 0; i &lt; l; i += 1) {
  110. v = o[i];
  111. switch (typeof v) {
  112. case &quot;undefined&quot;:
  113. case &quot;function&quot;:
  114. case &quot;unknown&quot;:
  115. break;
  116. default:
  117. if (b) {
  118. a.push(',');
  119. }
  120. a.push(v === null ? &quot;null&quot; : Ext.util.JSON.encode(v));
  121. b = true;
  122. }
  123. }
  124. a.push(&quot;]&quot;);
  125. return a.join(&quot;&quot;);
  126. };
  127. <span id='Ext-util-JSON-method-encodeDate'> /**
  128. </span> * &lt;p&gt;Encodes a Date. This returns the actual string which is inserted into the JSON string as the literal expression.
  129. * &lt;b&gt;The returned value includes enclosing double quotation marks.&lt;/b&gt;&lt;/p&gt;
  130. * &lt;p&gt;The default return format is &quot;yyyy-mm-ddThh:mm:ss&quot;.&lt;/p&gt;
  131. * &lt;p&gt;To override this:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;
  132. Ext.util.JSON.encodeDate = function(d) {
  133. return d.format('&quot;Y-m-d&quot;');
  134. };
  135. &lt;/code&gt;&lt;/pre&gt;
  136. * @param {Date} d The Date to encode
  137. * @return {String} The string literal to use in a JSON string.
  138. */
  139. this.encodeDate = function(o){
  140. return '&quot;' + o.getFullYear() + &quot;-&quot; +
  141. pad(o.getMonth() + 1) + &quot;-&quot; +
  142. pad(o.getDate()) + &quot;T&quot; +
  143. pad(o.getHours()) + &quot;:&quot; +
  144. pad(o.getMinutes()) + &quot;:&quot; +
  145. pad(o.getSeconds()) + '&quot;';
  146. };
  147. <span id='Ext-util-JSON-method-encode'> /**
  148. </span> * Encodes an Object, Array or other value
  149. * @param {Mixed} o The variable to encode
  150. * @return {String} The JSON string
  151. */
  152. this.encode = function() {
  153. var ec;
  154. return function(o) {
  155. if (!ec) {
  156. // setup encoding function on first access
  157. ec = isNative() ? JSON.stringify : doEncode;
  158. }
  159. return ec(o);
  160. };
  161. }();
  162. <span id='Ext-util-JSON-method-decode'> /**
  163. </span> * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError unless the safe option is set.
  164. * @param {String} json The JSON string
  165. * @return {Object} The resulting object
  166. */
  167. this.decode = function() {
  168. var dc;
  169. return function(json) {
  170. if (!dc) {
  171. // setup decoding function on first access
  172. dc = isNative() ? JSON.parse : doDecode;
  173. }
  174. return dc(json);
  175. };
  176. }();
  177. })();
  178. <span id='Ext-method-encode'>/**
  179. </span> * Shorthand for {@link Ext.util.JSON#encode}
  180. * @param {Mixed} o The variable to encode
  181. * @return {String} The JSON string
  182. * @member Ext
  183. * @method encode
  184. */
  185. Ext.encode = Ext.util.JSON.encode;
  186. <span id='Ext-method-decode'>/**
  187. </span> * Shorthand for {@link Ext.util.JSON#decode}
  188. * @param {String} json The JSON string
  189. * @param {Boolean} safe (optional) Whether to return null or throw an exception if the JSON is invalid.
  190. * @return {Object} The resulting object
  191. * @member Ext
  192. * @method decode
  193. */
  194. Ext.decode = Ext.util.JSON.decode;
  195. </pre>
  196. </body>
  197. </html>