Diffstat (limited to 'frontend/beta/js/YUI-extensions/JSON.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/YUI-extensions/JSON.js | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/frontend/beta/js/YUI-extensions/JSON.js b/frontend/beta/js/YUI-extensions/JSON.js new file mode 100644 index 0000000..ffc9573 --- a/dev/null +++ b/frontend/beta/js/YUI-extensions/JSON.js | |||
@@ -0,0 +1,132 @@ | |||
1 | /** | ||
2 | * @class YAHOO.ext.util.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 | ||
6 | * @singleton | ||
7 | */ | ||
8 | YAHOO.ext.util.JSON = new function(){ | ||
9 | var useHasOwn = {}.hasOwnProperty ? true : false; | ||
10 | |||
11 | // crashes Safari in some instances | ||
12 | //var validRE = /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/; | ||
13 | |||
14 | var pad = function(n) { | ||
15 | return n < 10 ? '0' + n : n; | ||
16 | }; | ||
17 | |||
18 | var m = { | ||
19 | '\b': '\\b', | ||
20 | '\t': '\\t', | ||
21 | '\n': '\\n', | ||
22 | '\f': '\\f', | ||
23 | '\r': '\\r', | ||
24 | '"' : '\\"', | ||
25 | '\\': '\\\\' | ||
26 | }; | ||
27 | |||
28 | var encodeString = function(s){ | ||
29 | if (/["\\\x00-\x1f]/.test(s)) { | ||
30 | return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) { | ||
31 | var c = m[b]; | ||
32 | if(c){ | ||
33 | return c; | ||
34 | } | ||
35 | c = b.charCodeAt(); | ||
36 | return '\\u00' + | ||
37 | Math.floor(c / 16).toString(16) + | ||
38 | (c % 16).toString(16); | ||
39 | }) + '"'; | ||
40 | } | ||
41 | return '"' + s + '"'; | ||
42 | }; | ||
43 | |||
44 | var encodeArray = function(o){ | ||
45 | var a = ['['], b, i, l = o.length, v; | ||
46 | for (i = 0; i < l; i += 1) { | ||
47 | v = o[i]; | ||
48 | switch (typeof v) { | ||
49 | case 'undefined': | ||
50 | case 'function': | ||
51 | case 'unknown': | ||
52 | break; | ||
53 | default: | ||
54 | if (b) { | ||
55 | a.push(','); | ||
56 | } | ||
57 | a.push(v === null ? "null" : YAHOO.ext.util.JSON.encode(v)); | ||
58 | b = true; | ||
59 | } | ||
60 | } | ||
61 | a.push(']'); | ||
62 | return a.join(''); | ||
63 | }; | ||
64 | |||
65 | var encodeDate = function(o){ | ||
66 | return '"' + o.getFullYear() + '-' + | ||
67 | pad(o.getMonth() + 1) + '-' + | ||
68 | pad(o.getDate()) + 'T' + | ||
69 | pad(o.getHours()) + ':' + | ||
70 | pad(o.getMinutes()) + ':' + | ||
71 | pad(o.getSeconds()) + '"'; | ||
72 | }; | ||
73 | |||
74 | /** | ||
75 | * Encodes an Object, Array or other value | ||
76 | * @param {Mixed} o The variable to encode | ||
77 | * @return {String} The JSON string | ||
78 | */ | ||
79 | this.encode = function(o){ | ||
80 | if(typeof o == 'undefined' || o === null){ | ||
81 | return 'null'; | ||
82 | }else if(o instanceof Array){ | ||
83 | return encodeArray(o); | ||
84 | }else if(o instanceof Date){ | ||
85 | return encodeDate(o); | ||
86 | }else if(typeof o == 'string'){ | ||
87 | return encodeString(o); | ||
88 | }else if(typeof o == 'number'){ | ||
89 | return isFinite(o) ? String(o) : "null"; | ||
90 | }else if(typeof o == 'boolean'){ | ||
91 | return String(o); | ||
92 | }else { | ||
93 | var a = ['{'], b, i, v; | ||
94 | for (var i in o) { | ||
95 | if(!useHasOwn || o.hasOwnProperty(i)) { | ||
96 | v = o[i]; | ||
97 | switch (typeof v) { | ||
98 | case 'undefined': | ||
99 | case 'function': | ||
100 | case 'unknown': | ||
101 | break; | ||
102 | default: | ||
103 | if(b){ | ||
104 | a.push(','); | ||
105 | } | ||
106 | a.push(this.encode(i), ':', | ||
107 | v === null ? "null" : this.encode(v)); | ||
108 | b = true; | ||
109 | } | ||
110 | } | ||
111 | } | ||
112 | a.push('}'); | ||
113 | return a.join(''); | ||
114 | } | ||
115 | }; | ||
116 | |||
117 | /** | ||
118 | * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError. | ||
119 | * @param {String} json The JSON string | ||
120 | * @return {Object} The resulting object | ||
121 | */ | ||
122 | this.decode = function(json){ | ||
123 | // although crockford had a good idea, this line crashes safari in some instances | ||
124 | //try{ | ||
125 | //if(validRE.test(json)) { | ||
126 | return eval('(' + json + ')'); | ||
127 | // } | ||
128 | // }catch(e){ | ||
129 | // } | ||
130 | // throw new SyntaxError("parseJSON"); | ||
131 | }; | ||
132 | }(); | ||