4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-JSON'>/**
19 </span> * @class Ext.JSON
20 * Modified version of Douglas Crockford"s json.js that doesn"t
21 * mess with the Object prototype
22 * http://www.json.org/js.html
25 Ext.JSON = new(function() {
26 var useHasOwn = !! {}.hasOwnProperty,
27 isNative = function() {
31 if (useNative === null) {
32 useNative = Ext.USE_NATIVE_JSON && window.JSON && JSON.toString() == '[object JSON]';
39 return n < 10 ? "0" + n : n;
41 doDecode = function(json) {
42 return eval("(" + json + ')');
44 doEncode = function(o) {
45 if (!Ext.isDefined(o) || o === null) {
46 return "null";
47 } else if (Ext.isArray(o)) {
48 return encodeArray(o);
49 } else if (Ext.isDate(o)) {
50 return Ext.JSON.encodeDate(o);
51 } else if (Ext.isString(o)) {
52 return encodeString(o);
53 } else if (typeof o == "number") {
54 //don't use isNumber here, since finite checks happen inside isNumber
55 return isFinite(o) ? String(o) : "null";
56 } else if (Ext.isBoolean(o)) {
58 } else if (Ext.isObject(o)) {
59 return encodeObject(o);
60 } else if (typeof o === "function") {
61 return "null";
66 "\b": '\\b',
67 "\t": '\\t',
68 "\n": '\\n',
69 "\f": '\\f',
70 "\r": '\\r',
72 "\\": '\\\\',
73 '\x0b': '\\u000b' //ie doesn't handle \v
75 charToReplace = /[\\\"\x00-\x1f\x7f-\uffff]/g,
76 encodeString = function(s) {
77 return '"' + s.replace(charToReplace, function(a) {
79 return typeof c === 'string' ? c : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
82 encodeArray = function(o) {
83 var a = ["[", ""],
84 // Note empty string in case there are no serializable members.
87 for (i = 0; i < len; i += 1) {
88 a.push(doEncode(o[i]), ',');
90 // Overwrite trailing comma (or empty string)
91 a[a.length - 1] = ']';
92 return a.join("");
94 encodeObject = function(o) {
95 var a = ["{", ""],
96 // Note empty string in case there are no serializable members.
99 if (!useHasOwn || o.hasOwnProperty(i)) {
100 a.push(doEncode(i), ":", doEncode(o[i]), ',');
103 // Overwrite trailing comma (or empty string)
104 a[a.length - 1] = '}';
105 return a.join("");
108 <span id='Ext-JSON-method-encodeDate'> /**
109 </span> * <p>Encodes a Date. This returns the actual string which is inserted into the JSON string as the literal expression.
110 * <b>The returned value includes enclosing double quotation marks.</b></p>
111 * <p>The default return format is "yyyy-mm-ddThh:mm:ss".</p>
112 * <p>To override this:</p><pre><code>
113 Ext.JSON.encodeDate = function(d) {
114 return d.format('"Y-m-d"');
116 </code></pre>
117 * @param {Date} d The Date to encode
118 * @return {String} The string literal to use in a JSON string.
120 this.encodeDate = function(o) {
121 return '"' + o.getFullYear() + "-"
122 + pad(o.getMonth() + 1) + "-"
123 + pad(o.getDate()) + "T"
124 + pad(o.getHours()) + ":"
125 + pad(o.getMinutes()) + ":"
126 + pad(o.getSeconds()) + '"';
129 <span id='Ext-JSON-method-encode'> /**
130 </span> * Encodes an Object, Array or other value
131 * @param {Mixed} o The variable to encode
132 * @return {String} The JSON string
134 this.encode = function() {
138 // setup encoding function on first access
139 ec = isNative() ? JSON.stringify : doEncode;
146 <span id='Ext-JSON-method-decode'> /**
147 </span> * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError unless the safe option is set.
148 * @param {String} json The JSON string
149 * @param {Boolean} safe (optional) Whether to return null or throw an exception if the JSON is invalid.
150 * @return {Object} The resulting object
152 this.decode = function() {
154 return function(json, safe) {
156 // setup decoding function on first access
157 dc = isNative() ? JSON.parse : doDecode;
166 sourceClass: "Ext.JSON",
167 sourceMethod: "decode",
168 msg: "You're trying to decode an invalid JSON String: " + json
175 <span id='Ext-method-encode'>/**
176 </span> * Shorthand for {@link Ext.JSON#encode}
177 * @param {Mixed} o The variable to encode
178 * @return {String} The JSON string
182 Ext.encode = Ext.JSON.encode;
183 <span id='Ext-method-decode'>/**
184 </span> * Shorthand for {@link Ext.JSON#decode}
185 * @param {String} json The JSON string
186 * @param {Boolean} safe (optional) Whether to return null or throw an exception if the JSON is invalid.
187 * @return {Object} The resulting object
191 Ext.decode = Ext.JSON.decode;