3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.util.JSON"></div>/**
10 * Modified version of Douglas Crockford"s json.js that doesn"t
11 * mess with the Object prototype
12 * http://www.json.org/js.html
15 Ext.util.JSON = new (function(){
16 var useHasOwn = !!{}.hasOwnProperty,
17 isNative = function() {
21 if (useNative === null) {
22 useNative = Ext.USE_NATIVE_JSON && window.JSON && JSON.toString() == '[object JSON]';
29 return n < 10 ? "0" + n : n;
31 doDecode = function(json){
32 return eval("(" + json + ')');
34 doEncode = function(o){
35 if(typeof o == "undefined" || o === null){
37 }else if(Ext.isArray(o)){
38 return encodeArray(o);
39 }else if(Object.prototype.toString.apply(o) === '[object Date]'){
40 return Ext.util.JSON.encodeDate(o);
41 }else if(typeof o == "string"){
42 return encodeString(o);
43 }else if(typeof o == "number"){
44 return isFinite(o) ? String(o) : "null";
45 }else if(typeof o == "boolean"){
48 var a = ["{"], b, i, v;
50 if(!useHasOwn || o.hasOwnProperty(i)) {
61 a.push(doEncode(i), ":",
62 v === null ? "null" : doEncode(v));
80 encodeString = function(s){
81 if (/["\\\x00-\x1f]/.test(s)) {
82 return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
89 Math.floor(c / 16).toString(16) +
90 (c % 16).toString(16);
95 encodeArray = function(o){
96 var a = ["["], b, i, l = o.length, v;
97 for (i = 0; i < l; i += 1) {
108 a.push(v === null ? "null" : Ext.util.JSON.encode(v));
116 this.encodeDate = function(o){
117 return '"' + o.getFullYear() + "-" +
118 pad(o.getMonth() + 1) + "-" +
119 pad(o.getDate()) + "T" +
120 pad(o.getHours()) + ":" +
121 pad(o.getMinutes()) + ":" +
122 pad(o.getSeconds()) + '"';
125 <div id="method-Ext.util.JSON-encode"></div>/**
126 * Encodes an Object, Array or other value
127 * @param {Mixed} o The variable to encode
128 * @return {String} The JSON string
130 this.encode = function() {
134 // setup encoding function on first access
135 ec = isNative() ? JSON.stringify : doEncode;
142 <div id="method-Ext.util.JSON-decode"></div>/**
143 * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError unless the safe option is set.
144 * @param {String} json The JSON string
145 * @return {Object} The resulting object
147 this.decode = function() {
149 return function(json) {
151 // setup decoding function on first access
152 dc = isNative() ? JSON.parse : doDecode;
159 <div id="method-Ext-encode"></div>/**
160 * Shorthand for {@link Ext.util.JSON#encode}
161 * @param {Mixed} o The variable to encode
162 * @return {String} The JSON string
166 Ext.encode = Ext.util.JSON.encode;
167 <div id="method-Ext-decode"></div>/**
168 * Shorthand for {@link Ext.util.JSON#decode}
169 * @param {String} json The JSON string
170 * @param {Boolean} safe (optional) Whether to return null or throw an exception if the JSON is invalid.
171 * @return {Object} The resulting object
175 Ext.decode = Ext.util.JSON.decode;