1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-JSON'>/**
2 </span> * @class Ext.JSON
3 * Modified version of Douglas Crockford"s json.js that doesn"t
4 * mess with the Object prototype
5 * http://www.json.org/js.html
8 Ext.JSON = new(function() {
9 var useHasOwn = !! {}.hasOwnProperty,
10 isNative = function() {
14 if (useNative === null) {
15 useNative = Ext.USE_NATIVE_JSON && window.JSON && JSON.toString() == '[object JSON]';
22 return n < 10 ? "0" + n : n;
24 doDecode = function(json) {
25 return eval("(" + json + ')');
27 doEncode = function(o) {
28 if (!Ext.isDefined(o) || o === null) {
29 return "null";
30 } else if (Ext.isArray(o)) {
31 return encodeArray(o);
32 } else if (Ext.isDate(o)) {
33 return Ext.JSON.encodeDate(o);
34 } else if (Ext.isString(o)) {
35 return encodeString(o);
36 } else if (typeof o == "number") {
37 //don't use isNumber here, since finite checks happen inside isNumber
38 return isFinite(o) ? String(o) : "null";
39 } else if (Ext.isBoolean(o)) {
41 } else if (Ext.isObject(o)) {
42 return encodeObject(o);
43 } else if (typeof o === "function") {
44 return "null";
49 "\b": '\\b',
50 "\t": '\\t',
51 "\n": '\\n',
52 "\f": '\\f',
53 "\r": '\\r',
55 "\\": '\\\\',
56 '\x0b': '\\u000b' //ie doesn't handle \v
58 charToReplace = /[\\\"\x00-\x1f\x7f-\uffff]/g,
59 encodeString = function(s) {
60 return '"' + s.replace(charToReplace, function(a) {
62 return typeof c === 'string' ? c : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
65 encodeArray = function(o) {
66 var a = ["[", ""],
67 // Note empty string in case there are no serializable members.
70 for (i = 0; i < len; i += 1) {
71 a.push(doEncode(o[i]), ',');
73 // Overwrite trailing comma (or empty string)
74 a[a.length - 1] = ']';
75 return a.join("");
77 encodeObject = function(o) {
78 var a = ["{", ""],
79 // Note empty string in case there are no serializable members.
82 if (!useHasOwn || o.hasOwnProperty(i)) {
83 a.push(doEncode(i), ":", doEncode(o[i]), ',');
86 // Overwrite trailing comma (or empty string)
87 a[a.length - 1] = '}';
88 return a.join("");
91 <span id='Ext-JSON-method-encodeDate'> /**
92 </span> * <p>Encodes a Date. This returns the actual string which is inserted into the JSON string as the literal expression.
93 * <b>The returned value includes enclosing double quotation marks.</b></p>
94 * <p>The default return format is "yyyy-mm-ddThh:mm:ss".</p>
95 * <p>To override this:</p><pre><code>
96 Ext.JSON.encodeDate = function(d) {
97 return d.format('"Y-m-d"');
99 </code></pre>
100 * @param {Date} d The Date to encode
101 * @return {String} The string literal to use in a JSON string.
103 this.encodeDate = function(o) {
104 return '"' + o.getFullYear() + "-"
105 + pad(o.getMonth() + 1) + "-"
106 + pad(o.getDate()) + "T"
107 + pad(o.getHours()) + ":"
108 + pad(o.getMinutes()) + ":"
109 + pad(o.getSeconds()) + '"';
112 <span id='Ext-JSON-method-encode'> /**
113 </span> * Encodes an Object, Array or other value
114 * @param {Mixed} o The variable to encode
115 * @return {String} The JSON string
117 this.encode = function() {
121 // setup encoding function on first access
122 ec = isNative() ? JSON.stringify : doEncode;
129 <span id='Ext-JSON-method-decode'> /**
130 </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.
131 * @param {String} json The JSON string
132 * @param {Boolean} safe (optional) Whether to return null or throw an exception if the JSON is invalid.
133 * @return {Object} The resulting object
135 this.decode = function() {
137 return function(json, safe) {
139 // setup decoding function on first access
140 dc = isNative() ? JSON.parse : doDecode;
149 sourceClass: "Ext.JSON",
150 sourceMethod: "decode",
151 msg: "You're trying to decode and invalid JSON String: " + json
158 <span id='Ext-method-encode'>/**
159 </span> * Shorthand for {@link Ext.JSON#encode}
160 * @param {Mixed} o The variable to encode
161 * @return {String} The JSON string
165 Ext.encode = Ext.JSON.encode;
166 <span id='Ext-method-decode'>/**
167 </span> * Shorthand for {@link Ext.JSON#decode}
168 * @param {String} json The JSON string
169 * @param {Boolean} safe (optional) Whether to return null or throw an exception if the JSON is invalid.
170 * @return {Object} The resulting object
174 Ext.decode = Ext.JSON.decode;
176 </pre></pre></body></html>