3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.util.JSON"></div>/**
10 * @class Ext.util.JSON
11 * Modified version of Douglas Crockford"s json.js that doesn"t
12 * mess with the Object prototype
13 * http://www.json.org/js.html
16 Ext.util.JSON = new (function(){
17 var useHasOwn = !!{}.hasOwnProperty,
18 isNative = function() {
22 if (useNative === null) {
23 useNative = Ext.USE_NATIVE_JSON && window.JSON && JSON.toString() == '[object JSON]';
30 return n < 10 ? "0" + n : n;
32 doDecode = function(json){
33 return eval("(" + json + ')');
35 doEncode = function(o){
36 if(!Ext.isDefined(o) || o === null){
38 }else if(Ext.isArray(o)){
39 return encodeArray(o);
40 }else if(Ext.isDate(o)){
41 return Ext.util.JSON.encodeDate(o);
42 }else if(Ext.isString(o)){
43 return encodeString(o);
44 }else if(typeof o == "number"){
45 //don't use isNumber here, since finite checks happen inside isNumber
46 return isFinite(o) ? String(o) : "null";
47 }else if(Ext.isBoolean(o)){
50 var a = ["{"], b, i, v;
52 // don't encode DOM objects
53 if(!o.getElementsByTagName){
54 if(!useHasOwn || o.hasOwnProperty(i)) {
65 a.push(doEncode(i), ":",
66 v === null ? "null" : doEncode(v));
85 encodeString = function(s){
86 if (/["\\\x00-\x1f]/.test(s)) {
87 return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
94 Math.floor(c / 16).toString(16) +
95 (c % 16).toString(16);
100 encodeArray = function(o){
101 var a = ["["], b, i, l = o.length, v;
102 for (i = 0; i < l; i += 1) {
113 a.push(v === null ? "null" : Ext.util.JSON.encode(v));
121 <div id="method-Ext.util.JSON-encodeDate"></div>/**
122 * <p>Encodes a Date. This returns the actual string which is inserted into the JSON string as the literal expression.
123 * <b>The returned value includes enclosing double quotation marks.</b></p>
124 * <p>The default return format is "yyyy-mm-ddThh:mm:ss".</p>
125 * <p>To override this:</p><pre><code>
126 Ext.util.JSON.encodeDate = function(d) {
127 return d.format('"Y-m-d"');
130 * @param {Date} d The Date to encode
131 * @return {String} The string literal to use in a JSON string.
133 this.encodeDate = function(o){
134 return '"' + o.getFullYear() + "-" +
135 pad(o.getMonth() + 1) + "-" +
136 pad(o.getDate()) + "T" +
137 pad(o.getHours()) + ":" +
138 pad(o.getMinutes()) + ":" +
139 pad(o.getSeconds()) + '"';
142 <div id="method-Ext.util.JSON-encode"></div>/**
143 * Encodes an Object, Array or other value
144 * @param {Mixed} o The variable to encode
145 * @return {String} The JSON string
147 this.encode = function() {
151 // setup encoding function on first access
152 ec = isNative() ? JSON.stringify : doEncode;
159 <div id="method-Ext.util.JSON-decode"></div>/**
160 * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError unless the safe option is set.
161 * @param {String} json The JSON string
162 * @return {Object} The resulting object
164 this.decode = function() {
166 return function(json) {
168 // setup decoding function on first access
169 dc = isNative() ? JSON.parse : doDecode;
176 <div id="method-Ext-encode"></div>/**
177 * Shorthand for {@link Ext.util.JSON#encode}
178 * @param {Mixed} o The variable to encode
179 * @return {String} The JSON string
183 Ext.encode = Ext.util.JSON.encode;
184 <div id="method-Ext-decode"></div>/**
185 * Shorthand for {@link Ext.util.JSON#decode}
186 * @param {String} json The JSON string
187 * @param {Boolean} safe (optional) Whether to return null or throw an exception if the JSON is invalid.
188 * @return {Object} The resulting object
192 Ext.decode = Ext.util.JSON.decode;