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