3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.util.JSON"></div>/**
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
16 Ext.util.JSON = new (function(){
17 var useHasOwn = !!{}.hasOwnProperty,
18 isNative = function() {
22 if (useNative === null) {
23 useNative = Ext.USE_NATIVE_JSON && window.JSON && JSON.toString() == '[object JSON]';
30 return n < 10 ? "0" + n : n;
32 doDecode = function(json){
33 return eval("(" + json + ')');
35 doEncode = function(o){
36 if(!Ext.isDefined(o) || o === null){
38 }else if(Ext.isArray(o)){
39 return encodeArray(o);
40 }else if(Ext.isDate(o)){
41 return Ext.util.JSON.encodeDate(o);
42 }else if(Ext.isString(o)){
43 return encodeString(o);
44 }else if(typeof o == "number"){
45 //don't use isNumber here, since finite checks happen inside isNumber
46 return isFinite(o) ? String(o) : "null";
47 }else if(Ext.isBoolean(o)){
50 var a = ["{"], b, i, v;
52 // don't encode DOM objects
53 if(!o.getElementsByTagName){
54 if(!useHasOwn || o.hasOwnProperty(i)) {
65 a.push(doEncode(i), ":",
66 v === null ? "null" : doEncode(v));
85 encodeString = function(s){
86 if (/["\\\x00-\x1f]/.test(s)) {
87 return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
94 Math.floor(c / 16).toString(16) +
95 (c % 16).toString(16);
100 encodeArray = function(o){
101 var a = ["["], b, i, l = o.length, v;
102 for (i = 0; i < l; i += 1) {
113 a.push(v === null ? "null" : Ext.util.JSON.encode(v));
121 this.encodeDate = function(o){
122 return '"' + o.getFullYear() + "-" +
123 pad(o.getMonth() + 1) + "-" +
124 pad(o.getDate()) + "T" +
125 pad(o.getHours()) + ":" +
126 pad(o.getMinutes()) + ":" +
127 pad(o.getSeconds()) + '"';
130 <div id="method-Ext.util.JSON-encode"></div>/**
131 * Encodes an Object, Array or other value
132 * @param {Mixed} o The variable to encode
133 * @return {String} The JSON string
135 this.encode = function() {
139 // setup encoding function on first access
140 ec = isNative() ? JSON.stringify : doEncode;
147 <div id="method-Ext.util.JSON-decode"></div>/**
148 * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError unless the safe option is set.
149 * @param {String} json The JSON string
150 * @return {Object} The resulting object
152 this.decode = function() {
154 return function(json) {
156 // setup decoding function on first access
157 dc = isNative() ? JSON.parse : doDecode;
164 <div id="method-Ext-encode"></div>/**
165 * Shorthand for {@link Ext.util.JSON#encode}
166 * @param {Mixed} o The variable to encode
167 * @return {String} The JSON string
171 Ext.encode = Ext.util.JSON.encode;
172 <div id="method-Ext-decode"></div>/**
173 * Shorthand for {@link Ext.util.JSON#decode}
174 * @param {String} json The JSON string
175 * @param {Boolean} safe (optional) Whether to return null or throw an exception if the JSON is invalid.
176 * @return {Object} The resulting object
180 Ext.decode = Ext.util.JSON.decode;