Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / util / core / JSON.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.util.JSON
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
12  * @singleton
13  */
14 Ext.util.JSON = new (function(){
15     var useHasOwn = !!{}.hasOwnProperty,
16         isNative = function() {
17             var useNative = null;
18
19             return function() {
20                 if (useNative === null) {
21                     useNative = Ext.USE_NATIVE_JSON && window.JSON && JSON.toString() == '[object JSON]';
22                 }
23         
24                 return useNative;
25             };
26         }(),
27         pad = function(n) {
28             return n < 10 ? "0" + n : n;
29         },
30         doDecode = function(json){
31             return eval("(" + json + ')');    
32         },
33         doEncode = function(o){
34             if(!Ext.isDefined(o) || o === null){
35                 return "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)){
46                 return String(o);
47             }else {
48                 var a = ["{"], b, i, v;
49                 for (i in o) {
50                     // don't encode DOM objects
51                     if(!o.getElementsByTagName){
52                         if(!useHasOwn || o.hasOwnProperty(i)) {
53                             v = o[i];
54                             switch (typeof v) {
55                             case "undefined":
56                             case "function":
57                             case "unknown":
58                                 break;
59                             default:
60                                 if(b){
61                                     a.push(',');
62                                 }
63                                 a.push(doEncode(i), ":",
64                                         v === null ? "null" : doEncode(v));
65                                 b = true;
66                             }
67                         }
68                     }
69                 }
70                 a.push("}");
71                 return a.join("");
72             }    
73         },
74         m = {
75             "\b": '\\b',
76             "\t": '\\t',
77             "\n": '\\n',
78             "\f": '\\f',
79             "\r": '\\r',
80             '"' : '\\"',
81             "\\": '\\\\'
82         },
83         encodeString = function(s){
84             if (/["\\\x00-\x1f]/.test(s)) {
85                 return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
86                     var c = m[b];
87                     if(c){
88                         return c;
89                     }
90                     c = b.charCodeAt();
91                     return "\\u00" +
92                         Math.floor(c / 16).toString(16) +
93                         (c % 16).toString(16);
94                 }) + '"';
95             }
96             return '"' + s + '"';
97         },
98         encodeArray = function(o){
99             var a = ["["], b, i, l = o.length, v;
100                 for (i = 0; i < l; i += 1) {
101                     v = o[i];
102                     switch (typeof v) {
103                         case "undefined":
104                         case "function":
105                         case "unknown":
106                             break;
107                         default:
108                             if (b) {
109                                 a.push(',');
110                             }
111                             a.push(v === null ? "null" : Ext.util.JSON.encode(v));
112                             b = true;
113                     }
114                 }
115                 a.push("]");
116                 return a.join("");
117         };
118
119     this.encodeDate = function(o){
120         return '"' + o.getFullYear() + "-" +
121                 pad(o.getMonth() + 1) + "-" +
122                 pad(o.getDate()) + "T" +
123                 pad(o.getHours()) + ":" +
124                 pad(o.getMinutes()) + ":" +
125                 pad(o.getSeconds()) + '"';
126     };
127
128     /**
129      * Encodes an Object, Array or other value
130      * @param {Mixed} o The variable to encode
131      * @return {String} The JSON string
132      */
133     this.encode = function() {
134         var ec;
135         return function(o) {
136             if (!ec) {
137                 // setup encoding function on first access
138                 ec = isNative() ? JSON.stringify : doEncode;
139             }
140             return ec(o);
141         };
142     }();
143
144
145     /**
146      * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError unless the safe option is set.
147      * @param {String} json The JSON string
148      * @return {Object} The resulting object
149      */
150     this.decode = function() {
151         var dc;
152         return function(json) {
153             if (!dc) {
154                 // setup decoding function on first access
155                 dc = isNative() ? JSON.parse : doDecode;
156             }
157             return dc(json);
158         };
159     }();
160
161 })();
162 /**
163  * Shorthand for {@link Ext.util.JSON#encode}
164  * @param {Mixed} o The variable to encode
165  * @return {String} The JSON string
166  * @member Ext
167  * @method encode
168  */
169 Ext.encode = Ext.util.JSON.encode;
170 /**
171  * Shorthand for {@link Ext.util.JSON#decode}
172  * @param {String} json The JSON string
173  * @param {Boolean} safe (optional) Whether to return null or throw an exception if the JSON is invalid.
174  * @return {Object} The resulting object
175  * @member Ext
176  * @method decode
177  */
178 Ext.decode = Ext.util.JSON.decode;