137 lines
No EOL
5.3 KiB
HTML
137 lines
No EOL
5.3 KiB
HTML
<html><head><title>JSON.js</title><link rel="stylesheet" type="text/css" href="../resources/style.css" media="screen"/></head><body><h1>JSON.js</h1><pre class="highlighted"><code><i>/**
|
|
* @class Ext.util.JSON
|
|
* Modified version of Douglas Crockford"s json.js that doesn"t
|
|
* mess <b>with</b> the Object prototype
|
|
* http:<i>//www.json.org/js.html</i>
|
|
* @singleton
|
|
*/</i>
|
|
Ext.util.JSON = <b>new</b> (<b>function</b>(){
|
|
<b>var</b> useHasOwn = {}.hasOwnProperty ? true : false;
|
|
|
|
<i>// crashes Safari <b>in</b> some instances</i>
|
|
<i>//<b>var</b> validRE = /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/;</i>
|
|
|
|
<b>var</b> pad = <b>function</b>(n) {
|
|
<b>return</b> n < 10 ? "0" + n : n;
|
|
};
|
|
|
|
<b>var</b> m = {
|
|
"\b": '\\b',
|
|
"\t": '\\t',
|
|
"\n": '\\n',
|
|
"\f": '\\f',
|
|
"\r": '\\r',
|
|
'"' : '\\"',
|
|
"\\": '\\\\'
|
|
};
|
|
|
|
<b>var</b> encodeString = <b>function</b>(s){
|
|
<b>if</b> (/["\\\x00-\x1f]/.test(s)) {
|
|
<b>return</b> '"' + s.replace(/([\x00-\x1f\\"])/g, <b>function</b>(a, b) {
|
|
<b>var</b> c = m[b];
|
|
<b>if</b>(c){
|
|
<b>return</b> c;
|
|
}
|
|
c = b.charCodeAt();
|
|
<b>return</b> "\\u00" +
|
|
Math.floor(c / 16).toString(16) +
|
|
(c % 16).toString(16);
|
|
}) + '"';
|
|
}
|
|
<b>return</b> '"' + s + '"';
|
|
};
|
|
|
|
<b>var</b> encodeArray = <b>function</b>(o){
|
|
<b>var</b> a = ["["], b, i, l = o.length, v;
|
|
<b>for</b> (i = 0; i < l; i += 1) {
|
|
v = o[i];
|
|
<b>switch</b> (<b>typeof</b> v) {
|
|
<b>case</b> "undefined":
|
|
<b>case</b> "<b>function</b>":
|
|
<b>case</b> "unknown":
|
|
<b>break</b>;
|
|
<b>default</b>:
|
|
<b>if</b> (b) {
|
|
a.push(',');
|
|
}
|
|
a.push(v === null ? "null" : Ext.util.JSON.encode(v));
|
|
b = true;
|
|
}
|
|
}
|
|
a.push("]");
|
|
<b>return</b> a.join("");
|
|
};
|
|
|
|
<b>var</b> encodeDate = <b>function</b>(o){
|
|
<b>return</b> '"' + o.getFullYear() + "-" +
|
|
pad(o.getMonth() + 1) + "-" +
|
|
pad(o.getDate()) + "T" +
|
|
pad(o.getHours()) + ":" +
|
|
pad(o.getMinutes()) + ":" +
|
|
pad(o.getSeconds()) + '"';
|
|
};
|
|
|
|
<i>/**
|
|
* Encodes an Object, Array or other value
|
|
* @param {Mixed} o The variable to encode
|
|
* @<b>return</b> {String} The JSON string
|
|
*/</i>
|
|
<b>this</b>.encode = <b>function</b>(o){
|
|
<b>if</b>(typeof o == "undefined" || o === null){
|
|
<b>return</b> "null";
|
|
}<b>else</b> if(o instanceof Array){
|
|
<b>return</b> encodeArray(o);
|
|
}<b>else</b> if(o instanceof Date){
|
|
<b>return</b> encodeDate(o);
|
|
}<b>else</b> if(<b>typeof</b> o == "string"){
|
|
<b>return</b> encodeString(o);
|
|
}<b>else</b> if(<b>typeof</b> o == "number"){
|
|
<b>return</b> isFinite(o) ? String(o) : "null";
|
|
}<b>else</b> if(<b>typeof</b> o == "boolean"){
|
|
<b>return</b> String(o);
|
|
}<b>else</b> {
|
|
<b>var</b> a = ["{"], b, i, v;
|
|
<b>for</b> (i <b>in</b> o) {
|
|
<b>if</b>(!useHasOwn || o.hasOwnProperty(i)) {
|
|
v = o[i];
|
|
<b>switch</b> (<b>typeof</b> v) {
|
|
<b>case</b> "undefined":
|
|
<b>case</b> "<b>function</b>":
|
|
<b>case</b> "unknown":
|
|
<b>break</b>;
|
|
<b>default</b>:
|
|
<b>if</b>(b){
|
|
a.push(',');
|
|
}
|
|
a.push(<b>this</b>.encode(i), ":",
|
|
v === null ? "null" : <b>this</b>.encode(v));
|
|
b = true;
|
|
}
|
|
}
|
|
}
|
|
a.push("}");
|
|
<b>return</b> a.join("");
|
|
}
|
|
};
|
|
|
|
<i>/**
|
|
* Decodes (parses) a JSON string to an object. If the JSON is invalid, <b>this</b> function throws a SyntaxError.
|
|
* @param {String} json The JSON string
|
|
* @<b>return</b> {Object} The resulting object
|
|
*/</i>
|
|
<b>this</b>.decode = <b>function</b>(json){
|
|
<b>return</b> eval("(" + json + ')');
|
|
};
|
|
})();
|
|
<i>/**
|
|
* Shorthand <b>for</b> {@link Ext.util.JSON#encode}
|
|
* @member Ext encode
|
|
* @method */</i>
|
|
Ext.encode = Ext.util.JSON.encode;
|
|
<i>/**
|
|
* Shorthand <b>for</b> {@link Ext.util.JSON#decode}
|
|
* @member Ext decode
|
|
* @method */</i>
|
|
Ext.decode = Ext.util.JSON.decode;
|
|
</code></pre><hr><div style="font-size:10px;text-align:center;color:gray;">Ext - Copyright © 2006-2007 Ext JS, LLC<br />All rights reserved.</div>
|
|
</body></html> |