Fix #6913 - add more documentation to code
[roojs1] / Roo / util / JSON.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11 /**
12  * @class Roo.util.JSON
13  * Modified version of Douglas Crockford"s json.js that doesn"t
14  * mess with the Object prototype 
15  * http://www.json.org/js.html
16  * @static
17  */
18 Roo.util.JSON = new (function(){
19     var useHasOwn = {}.hasOwnProperty ? true : false;
20     
21     // crashes Safari in some instances
22     //var validRE = /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/;
23     
24     var pad = function(n) {
25         return n < 10 ? "0" + n : n;
26     };
27     
28     var m = {
29         "\b": '\\b',
30         "\t": '\\t',
31         "\n": '\\n',
32         "\f": '\\f',
33         "\r": '\\r',
34         '"' : '\\"',
35         "\\": '\\\\'
36     };
37
38     var encodeString = function(s){
39         if (/["\\\x00-\x1f]/.test(s)) {
40             return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
41                 var c = m[b];
42                 if(c){
43                     return c;
44                 }
45                 c = b.charCodeAt();
46                 return "\\u00" +
47                     Math.floor(c / 16).toString(16) +
48                     (c % 16).toString(16);
49             }) + '"';
50         }
51         return '"' + s + '"';
52     };
53     
54     var encodeArray = function(o){
55         var a = ["["], b, i, l = o.length, v;
56             for (i = 0; i < l; i += 1) {
57                 v = o[i];
58                 switch (typeof v) {
59                     case "undefined":
60                     case "function":
61                     case "unknown":
62                         break;
63                     default:
64                         if (b) {
65                             a.push(',');
66                         }
67                         a.push(v === null ? "null" : Roo.util.JSON.encode(v));
68                         b = true;
69                 }
70             }
71             a.push("]");
72             return a.join("");
73     };
74     
75     var encodeDate = function(o){
76         return '"' + o.getFullYear() + "-" +
77                 pad(o.getMonth() + 1) + "-" +
78                 pad(o.getDate()) + "T" +
79                 pad(o.getHours()) + ":" +
80                 pad(o.getMinutes()) + ":" +
81                 pad(o.getSeconds()) + '"';
82     };
83     
84     /**
85      * Encodes an Object, Array or other value
86      * @param {Mixed} o The variable to encode
87      * @return {String} The JSON string
88      */
89     this.encode = function(o)
90     {
91         // should this be extended to fully wrap stringify..
92         
93         if(typeof o == "undefined" || o === null){
94             return "null";
95         }else if(o instanceof Array){
96             return encodeArray(o);
97         }else if(o instanceof Date){
98             return encodeDate(o);
99         }else if(typeof o == "string"){
100             return encodeString(o);
101         }else if(typeof o == "number"){
102             return isFinite(o) ? String(o) : "null";
103         }else if(typeof o == "boolean"){
104             return String(o);
105         }else {
106             var a = ["{"], b, i, v;
107             for (i in o) {
108                 if(!useHasOwn || o.hasOwnProperty(i)) {
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(this.encode(i), ":",
120                                 v === null ? "null" : this.encode(v));
121                         b = true;
122                     }
123                 }
124             }
125             a.push("}");
126             return a.join("");
127         }
128     };
129     
130     /**
131      * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError.
132      * @param {String} json The JSON string
133      * @return {Object} The resulting object
134      */
135     this.decode = function(json){
136         
137         return  /** eval:var:json */ eval("(" + json + ')');
138     };
139 })();
140 /** 
141  * Shorthand for {@link Roo.util.JSON#encode}
142  * @member Roo encode 
143  * @method */
144 Roo.encode = typeof(JSON) != 'undefined' && JSON.stringify ? JSON.stringify : Roo.util.JSON.encode;
145 /** 
146  * Shorthand for {@link Roo.util.JSON#decode}
147  * @member Roo decode 
148  * @method */
149 Roo.decode = typeof(JSON) != 'undefined' && JSON.parse ? JSON.parse : Roo.util.JSON.decode;