3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
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.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.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)) {
55 } else if (Ext.isObject(o)) {
56 return encodeObject(o);
57 } else if (typeof o === "function") {
70 '\x0b': '\\u000b' //ie doesn't handle \v
72 charToReplace = /[\\\"\x00-\x1f\x7f-\uffff]/g,
73 encodeString = function(s) {
74 return '"' + s.replace(charToReplace, function(a) {
76 return typeof c === 'string' ? c : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
79 encodeArray = function(o) {
81 // Note empty string in case there are no serializable members.
84 for (i = 0; i < len; i += 1) {
85 a.push(doEncode(o[i]), ',');
87 // Overwrite trailing comma (or empty string)
88 a[a.length - 1] = ']';
91 encodeObject = function(o) {
93 // Note empty string in case there are no serializable members.
96 if (!useHasOwn || o.hasOwnProperty(i)) {
97 a.push(doEncode(i), ":", doEncode(o[i]), ',');
100 // Overwrite trailing comma (or empty string)
101 a[a.length - 1] = '}';
106 * <p>Encodes a Date. This returns the actual string which is inserted into the JSON string as the literal expression.
107 * <b>The returned value includes enclosing double quotation marks.</b></p>
108 * <p>The default return format is "yyyy-mm-ddThh:mm:ss".</p>
109 * <p>To override this:</p><pre><code>
110 Ext.JSON.encodeDate = function(d) {
111 return Ext.Date.format(d, '"Y-m-d"');
114 * @param {Date} d The Date to encode
115 * @return {String} The string literal to use in a JSON string.
117 this.encodeDate = function(o) {
118 return '"' + o.getFullYear() + "-"
119 + pad(o.getMonth() + 1) + "-"
120 + pad(o.getDate()) + "T"
121 + pad(o.getHours()) + ":"
122 + pad(o.getMinutes()) + ":"
123 + pad(o.getSeconds()) + '"';
127 * Encodes an Object, Array or other value
128 * @param {Object} o The variable to encode
129 * @return {String} The JSON string
131 this.encode = function() {
135 // setup encoding function on first access
136 ec = isNative() ? JSON.stringify : doEncode;
144 * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError unless the safe option is set.
145 * @param {String} json The JSON string
146 * @param {Boolean} safe (optional) Whether to return null or throw an exception if the JSON is invalid.
147 * @return {Object} The resulting object
149 this.decode = function() {
151 return function(json, safe) {
153 // setup decoding function on first access
154 dc = isNative() ? JSON.parse : doDecode;
163 sourceClass: "Ext.JSON",
164 sourceMethod: "decode",
165 msg: "You're trying to decode an invalid JSON String: " + json
173 * Shorthand for {@link Ext.JSON#encode}
176 * @alias Ext.JSON#encode
178 Ext.encode = Ext.JSON.encode;
180 * Shorthand for {@link Ext.JSON#decode}
183 * @alias Ext.JSON#decode
185 Ext.decode = Ext.JSON.decode;