3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
9 * Modified version of Douglas Crockford"s json.js that doesn"t
10 * mess with the Object prototype
11 * http://www.json.org/js.html
14 Ext.util.JSON = new (function(){
15 var useHasOwn = !!{}.hasOwnProperty,
16 isNative = function() {
20 if (useNative === null) {
21 useNative = Ext.USE_NATIVE_JSON && window.JSON && JSON.toString() == '[object JSON]';
28 return n < 10 ? "0" + n : n;
30 doDecode = function(json){
31 return eval("(" + json + ')');
33 doEncode = function(o){
34 if(!Ext.isDefined(o) || o === null){
36 }else if(Ext.isArray(o)){
37 return encodeArray(o);
38 }else if(Ext.isDate(o)){
39 return Ext.util.JSON.encodeDate(o);
40 }else if(Ext.isString(o)){
41 return encodeString(o);
42 }else if(typeof o == "number"){
43 //don't use isNumber here, since finite checks happen inside isNumber
44 return isFinite(o) ? String(o) : "null";
45 }else if(Ext.isBoolean(o)){
48 var a = ["{"], b, i, v;
50 // don't encode DOM objects
51 if(!o.getElementsByTagName){
52 if(!useHasOwn || o.hasOwnProperty(i)) {
63 a.push(doEncode(i), ":",
64 v === null ? "null" : doEncode(v));
83 encodeString = function(s){
84 if (/["\\\x00-\x1f]/.test(s)) {
85 return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
92 Math.floor(c / 16).toString(16) +
93 (c % 16).toString(16);
98 encodeArray = function(o){
99 var a = ["["], b, i, l = o.length, v;
100 for (i = 0; i < l; i += 1) {
111 a.push(v === null ? "null" : Ext.util.JSON.encode(v));
120 * <p>Encodes a Date. This returns the actual string which is inserted into the JSON string as the literal expression.
121 * <b>The returned value includes enclosing double quotation marks.</b></p>
122 * <p>The default return format is "yyyy-mm-ddThh:mm:ss".</p>
123 * <p>To override this:</p><pre><code>
124 Ext.util.JSON.encodeDate = function(d) {
125 return d.format('"Y-m-d"');
128 * @param {Date} d The Date to encode
129 * @return {String} The string literal to use in a JSON string.
131 this.encodeDate = function(o){
132 return '"' + o.getFullYear() + "-" +
133 pad(o.getMonth() + 1) + "-" +
134 pad(o.getDate()) + "T" +
135 pad(o.getHours()) + ":" +
136 pad(o.getMinutes()) + ":" +
137 pad(o.getSeconds()) + '"';
141 * Encodes an Object, Array or other value
142 * @param {Mixed} o The variable to encode
143 * @return {String} The JSON string
145 this.encode = function() {
149 // setup encoding function on first access
150 ec = isNative() ? JSON.stringify : doEncode;
158 * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError unless the safe option is set.
159 * @param {String} json The JSON string
160 * @return {Object} The resulting object
162 this.decode = function() {
164 return function(json) {
166 // setup decoding function on first access
167 dc = isNative() ? JSON.parse : doDecode;
175 * Shorthand for {@link Ext.util.JSON#encode}
176 * @param {Mixed} o The variable to encode
177 * @return {String} The JSON string
181 Ext.encode = Ext.util.JSON.encode;
183 * Shorthand for {@link Ext.util.JSON#decode}
184 * @param {String} json The JSON string
185 * @param {Boolean} safe (optional) Whether to return null or throw an exception if the JSON is invalid.
186 * @return {Object} The resulting object
190 Ext.decode = Ext.util.JSON.decode;