initial import
[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  * @singleton
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         if(typeof o == "undefined" || o === null){
91             return "null";
92         }else if(o instanceof Array){
93             return encodeArray(o);
94         }else if(o instanceof Date){
95             return encodeDate(o);
96         }else if(typeof o == "string"){
97             return encodeString(o);
98         }else if(typeof o == "number"){
99             return isFinite(o) ? String(o) : "null";
100         }else if(typeof o == "boolean"){
101             return String(o);
102         }else {
103             var a = ["{"], b, i, v;
104             for (i in o) {
105                 if(!useHasOwn || o.hasOwnProperty(i)) {
106                     v = o[i];
107                     switch (typeof v) {
108                     case "undefined":
109                     case "function":
110                     case "unknown":
111                         break;
112                     default:
113                         if(b){
114                             a.push(',');
115                         }
116                         a.push(this.encode(i), ":",
117                                 v === null ? "null" : this.encode(v));
118                         b = true;
119                     }
120                 }
121             }
122             a.push("}");
123             return a.join("");
124         }
125     };
126     
127     /**
128      * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError.
129      * @param {String} json The JSON string
130      * @return {Object} The resulting object
131      */
132     this.decode = function(json){
133         /**
134          * eval:var:json
135          */
136         return eval("(" + json + ')');
137     };
138 })();
139 /** 
140  * Shorthand for {@link Roo.util.JSON#encode}
141  * @member Roo encode 
142  * @method */
143 Roo.encode = Roo.util.JSON.encode;
144 /** 
145  * Shorthand for {@link Roo.util.JSON#decode}
146  * @member Roo decode 
147  * @method */
148 Roo.decode = Roo.util.JSON.decode;