2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
10 * @class Ext.util.JSON
\r
11 * Modified version of Douglas Crockford"s json.js that doesn"t
\r
12 * mess with the Object prototype
\r
13 * http://www.json.org/js.html
\r
16 Ext.util.JSON = new (function(){
\r
17 var useHasOwn = !!{}.hasOwnProperty;
\r
19 // crashes Safari in some instances
\r
20 //var validRE = /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/;
\r
22 var pad = function(n) {
\r
23 return n < 10 ? "0" + n : n;
\r
36 var encodeString = function(s){
\r
37 if (/["\\\x00-\x1f]/.test(s)) {
\r
38 return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
\r
45 Math.floor(c / 16).toString(16) +
\r
46 (c % 16).toString(16);
\r
49 return '"' + s + '"';
\r
52 var encodeArray = function(o){
\r
53 var a = ["["], b, i, l = o.length, v;
\r
54 for (i = 0; i < l; i += 1) {
\r
65 a.push(v === null ? "null" : Ext.util.JSON.encode(v));
\r
73 this.encodeDate = function(o){
\r
74 return '"' + o.getFullYear() + "-" +
\r
75 pad(o.getMonth() + 1) + "-" +
\r
76 pad(o.getDate()) + "T" +
\r
77 pad(o.getHours()) + ":" +
\r
78 pad(o.getMinutes()) + ":" +
\r
79 pad(o.getSeconds()) + '"';
\r
83 * Encodes an Object, Array or other value
\r
84 * @param {Mixed} o The variable to encode
\r
85 * @return {String} The JSON string
\r
87 this.encode = function(o){
\r
88 if(typeof o == "undefined" || o === null){
\r
90 }else if(Ext.isArray(o)){
\r
91 return encodeArray(o);
\r
92 }else if(Ext.isDate(o)){
\r
93 return Ext.util.JSON.encodeDate(o);
\r
94 }else if(typeof o == "string"){
\r
95 return encodeString(o);
\r
96 }else if(typeof o == "number"){
\r
97 return isFinite(o) ? String(o) : "null";
\r
98 }else if(typeof o == "boolean"){
\r
101 var a = ["{"], b, i, v;
\r
103 if(!useHasOwn || o.hasOwnProperty(i)) {
\r
105 switch (typeof v) {
\r
114 a.push(this.encode(i), ":",
\r
115 v === null ? "null" : this.encode(v));
\r
126 * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError.
\r
127 * @param {String} json The JSON string
\r
128 * @return {Object} The resulting object
\r
130 this.decode = function(json){
\r
131 return eval("(" + json + ')');
\r
135 * Shorthand for {@link Ext.util.JSON#encode}
\r
136 * @param {Mixed} o The variable to encode
\r
137 * @return {String} The JSON string
\r
141 Ext.encode = Ext.util.JSON.encode;
\r
143 * Shorthand for {@link Ext.util.JSON#decode}
\r
144 * @param {String} json The JSON string
\r
145 * @return {Object} The resulting object
\r
149 Ext.decode = Ext.util.JSON.decode;
\r