3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.3.1
11 * Copyright(c) 2006-2010 Sencha Inc.
12 * licensing@sencha.com
13 * http://www.sencha.com/license
15 <div id="cls-Ext.util.JSON"></div>/**
16 * @class Ext.util.JSON
17 * Modified version of Douglas Crockford"s json.js that doesn"t
18 * mess with the Object prototype
19 * http://www.json.org/js.html
22 Ext.util.JSON = new (function(){
23 var useHasOwn = !!{}.hasOwnProperty,
24 isNative = function() {
28 if (useNative === null) {
29 useNative = Ext.USE_NATIVE_JSON && window.JSON && JSON.toString() == '[object JSON]';
36 return n < 10 ? "0" + n : n;
38 doDecode = function(json){
39 return eval("(" + json + ")");
41 doEncode = function(o){
42 if(!Ext.isDefined(o) || o === null){
44 }else if(Ext.isArray(o)){
45 return encodeArray(o);
46 }else if(Ext.isDate(o)){
47 return Ext.util.JSON.encodeDate(o);
48 }else if(Ext.isString(o)){
49 return encodeString(o);
50 }else if(typeof o == "number"){
51 //don't use isNumber here, since finite checks happen inside isNumber
52 return isFinite(o) ? String(o) : "null";
53 }else if(Ext.isBoolean(o)){
56 var a = ["{"], b, i, v;
58 // don't encode DOM objects
59 if(!o.getElementsByTagName){
60 if(!useHasOwn || o.hasOwnProperty(i)) {
71 a.push(doEncode(i), ":",
72 v === null ? "null" : doEncode(v));
91 encodeString = function(s){
92 if (/["\\\x00-\x1f]/.test(s)) {
93 return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
100 Math.floor(c / 16).toString(16) +
101 (c % 16).toString(16);
104 return '"' + s + '"';
106 encodeArray = function(o){
107 var a = ["["], b, i, l = o.length, v;
108 for (i = 0; i < l; i += 1) {
119 a.push(v === null ? "null" : Ext.util.JSON.encode(v));
127 <div id="method-Ext.util.JSON-encodeDate"></div>/**
128 * <p>Encodes a Date. This returns the actual string which is inserted into the JSON string as the literal expression.
129 * <b>The returned value includes enclosing double quotation marks.</b></p>
130 * <p>The default return format is "yyyy-mm-ddThh:mm:ss".</p>
131 * <p>To override this:</p><pre><code>
132 Ext.util.JSON.encodeDate = function(d) {
133 return d.format('"Y-m-d"');
136 * @param {Date} d The Date to encode
137 * @return {String} The string literal to use in a JSON string.
139 this.encodeDate = function(o){
140 return '"' + o.getFullYear() + "-" +
141 pad(o.getMonth() + 1) + "-" +
142 pad(o.getDate()) + "T" +
143 pad(o.getHours()) + ":" +
144 pad(o.getMinutes()) + ":" +
145 pad(o.getSeconds()) + '"';
148 <div id="method-Ext.util.JSON-encode"></div>/**
149 * Encodes an Object, Array or other value
150 * @param {Mixed} o The variable to encode
151 * @return {String} The JSON string
153 this.encode = function() {
157 // setup encoding function on first access
158 ec = isNative() ? JSON.stringify : doEncode;
165 <div id="method-Ext.util.JSON-decode"></div>/**
166 * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError unless the safe option is set.
167 * @param {String} json The JSON string
168 * @return {Object} The resulting object
170 this.decode = function() {
172 return function(json) {
174 // setup decoding function on first access
175 dc = isNative() ? JSON.parse : doDecode;
182 <div id="method-Ext-encode"></div>/**
183 * Shorthand for {@link Ext.util.JSON#encode}
184 * @param {Mixed} o The variable to encode
185 * @return {String} The JSON string
189 Ext.encode = Ext.util.JSON.encode;
190 <div id="method-Ext-decode"></div>/**
191 * Shorthand for {@link Ext.util.JSON#decode}
192 * @param {String} json The JSON string
193 * @param {Boolean} safe (optional) Whether to return null or throw an exception if the JSON is invalid.
194 * @return {Object} The resulting object
198 Ext.decode = Ext.util.JSON.decode;