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