3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.util.JSON"></div>/**
15 * @class Ext.util.JSON
16 * Modified version of Douglas Crockford"s json.js that doesn"t
17 * mess with the Object prototype
18 * http://www.json.org/js.html
21 Ext.util.JSON = new (function(){
22 var useHasOwn = !!{}.hasOwnProperty,
23 isNative = function() {
27 if (useNative === null) {
28 useNative = Ext.USE_NATIVE_JSON && window.JSON && JSON.toString() == '[object JSON]';
35 return n < 10 ? "0" + n : n;
37 doDecode = function(json){
38 return eval("(" + json + ')');
40 doEncode = function(o){
41 if(!Ext.isDefined(o) || o === null){
43 }else if(Ext.isArray(o)){
44 return encodeArray(o);
45 }else if(Ext.isDate(o)){
46 return Ext.util.JSON.encodeDate(o);
47 }else if(Ext.isString(o)){
48 return encodeString(o);
49 }else if(typeof o == "number"){
50 //don't use isNumber here, since finite checks happen inside isNumber
51 return isFinite(o) ? String(o) : "null";
52 }else if(Ext.isBoolean(o)){
55 var a = ["{"], b, i, v;
57 // don't encode DOM objects
58 if(!o.getElementsByTagName){
59 if(!useHasOwn || o.hasOwnProperty(i)) {
70 a.push(doEncode(i), ":",
71 v === null ? "null" : doEncode(v));
90 encodeString = function(s){
91 if (/["\\\x00-\x1f]/.test(s)) {
92 return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
99 Math.floor(c / 16).toString(16) +
100 (c % 16).toString(16);
103 return '"' + s + '"';
105 encodeArray = function(o){
106 var a = ["["], b, i, l = o.length, v;
107 for (i = 0; i < l; i += 1) {
118 a.push(v === null ? "null" : Ext.util.JSON.encode(v));
126 this.encodeDate = function(o){
127 return '"' + o.getFullYear() + "-" +
128 pad(o.getMonth() + 1) + "-" +
129 pad(o.getDate()) + "T" +
130 pad(o.getHours()) + ":" +
131 pad(o.getMinutes()) + ":" +
132 pad(o.getSeconds()) + '"';
135 <div id="method-Ext.util.JSON-encode"></div>/**
136 * Encodes an Object, Array or other value
137 * @param {Mixed} o The variable to encode
138 * @return {String} The JSON string
140 this.encode = function() {
144 // setup encoding function on first access
145 ec = isNative() ? JSON.stringify : doEncode;
152 <div id="method-Ext.util.JSON-decode"></div>/**
153 * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError unless the safe option is set.
154 * @param {String} json The JSON string
155 * @return {Object} The resulting object
157 this.decode = function() {
159 return function(json) {
161 // setup decoding function on first access
162 dc = isNative() ? JSON.parse : doDecode;
169 <div id="method-Ext-encode"></div>/**
170 * Shorthand for {@link Ext.util.JSON#encode}
171 * @param {Mixed} o The variable to encode
172 * @return {String} The JSON string
176 Ext.encode = Ext.util.JSON.encode;
177 <div id="method-Ext-decode"></div>/**
178 * Shorthand for {@link Ext.util.JSON#decode}
179 * @param {String} json The JSON string
180 * @param {Boolean} safe (optional) Whether to return null or throw an exception if the JSON is invalid.
181 * @return {Object} The resulting object
185 Ext.decode = Ext.util.JSON.decode;