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