* (bug 28560) list=deletedrevs should die, if combination of param is invalid
[lhc/web/wiklou.git] / resources / jquery / jquery.json.js
1 /*
2 * jQuery JSON Plugin
3 * version: 2.1 (2009-08-14)
4 *
5 * This document is licensed as free software under the terms of the
6 * MIT License: http://www.opensource.org/licenses/mit-license.php
7 *
8 * Brantley Harris wrote this plugin. It is based somewhat on the JSON.org
9 * website's http://www.json.org/json2.js, which proclaims:
10 * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that
11 * I uphold.
12 *
13 * It is also influenced heavily by MochiKit's serializeJSON, which is
14 * copyrighted 2005 by Bob Ippolito.
15 *
16 * @see http://code.google.com/p/jquery-json/
17 */
18
19 (function($) {
20 /** jQuery.toJSON( json-serializble )
21 Converts the given argument into a JSON respresentation.
22
23 If an object has a "toJSON" function, that will be used to get the representation.
24 Non-integer/string keys are skipped in the object, as are keys that point to a function.
25
26 json-serializble:
27 The *thing* to be converted.
28 **/
29 $.toJSON = function(o)
30 {
31 if (typeof(JSON) == 'object' && JSON.stringify)
32 return JSON.stringify(o);
33
34 var type = typeof(o);
35
36 if (o === null)
37 return "null";
38
39 if (type == "undefined")
40 return undefined;
41
42 if (type == "number" || type == "boolean")
43 return o + "";
44
45 if (type == "string")
46 return $.quoteString(o);
47
48 if (type == 'object')
49 {
50 if (typeof o.toJSON == "function")
51 return $.toJSON( o.toJSON() );
52
53 if (o.constructor === Date)
54 {
55 var month = o.getUTCMonth() + 1;
56 if (month < 10) month = '0' + month;
57
58 var day = o.getUTCDate();
59 if (day < 10) day = '0' + day;
60
61 var year = o.getUTCFullYear();
62
63 var hours = o.getUTCHours();
64 if (hours < 10) hours = '0' + hours;
65
66 var minutes = o.getUTCMinutes();
67 if (minutes < 10) minutes = '0' + minutes;
68
69 var seconds = o.getUTCSeconds();
70 if (seconds < 10) seconds = '0' + seconds;
71
72 var milli = o.getUTCMilliseconds();
73 if (milli < 100) milli = '0' + milli;
74 if (milli < 10) milli = '0' + milli;
75
76 return '"' + year + '-' + month + '-' + day + 'T' +
77 hours + ':' + minutes + ':' + seconds +
78 '.' + milli + 'Z"';
79 }
80
81 if (o.constructor === Array)
82 {
83 var ret = [];
84 for (var i = 0; i < o.length; i++)
85 ret.push( $.toJSON(o[i]) || "null" );
86
87 return "[" + ret.join(",") + "]";
88 }
89
90 var pairs = [];
91 for (var k in o) {
92 var name;
93 var type = typeof k;
94
95 if (type == "number")
96 name = '"' + k + '"';
97 else if (type == "string")
98 name = $.quoteString(k);
99 else
100 continue; //skip non-string or number keys
101
102 if (typeof o[k] == "function")
103 continue; //skip pairs where the value is a function.
104
105 var val = $.toJSON(o[k]);
106
107 pairs.push(name + ":" + val);
108 }
109
110 return "{" + pairs.join(", ") + "}";
111 }
112 };
113
114 /** jQuery.evalJSON(src)
115 Evaluates a given piece of json source.
116 **/
117 $.evalJSON = function(src)
118 {
119 if (typeof(JSON) == 'object' && JSON.parse)
120 return JSON.parse(src);
121 return eval("(" + src + ")");
122 };
123
124 /** jQuery.secureEvalJSON(src)
125 Evals JSON in a way that is *more* secure.
126 **/
127 $.secureEvalJSON = function(src)
128 {
129 if (typeof(JSON) == 'object' && JSON.parse)
130 return JSON.parse(src);
131
132 var filtered = src;
133 filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@');
134 filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
135 filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
136
137 if (/^[\],:{}\s]*$/.test(filtered))
138 return eval("(" + src + ")");
139 else
140 throw new SyntaxError("Error parsing JSON, source is not valid.");
141 };
142
143 /** jQuery.quoteString(string)
144 Returns a string-repr of a string, escaping quotes intelligently.
145 Mostly a support function for toJSON.
146
147 Examples:
148 >>> jQuery.quoteString("apple")
149 "apple"
150
151 >>> jQuery.quoteString('"Where are we going?", she asked.')
152 "\"Where are we going?\", she asked."
153 **/
154 $.quoteString = function(string)
155 {
156 if (string.match(_escapeable))
157 {
158 return '"' + string.replace(_escapeable, function (a)
159 {
160 var c = _meta[a];
161 if (typeof c === 'string') return c;
162 c = a.charCodeAt();
163 return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
164 }) + '"';
165 }
166 return '"' + string + '"';
167 };
168
169 var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;
170
171 var _meta = {
172 '\b': '\\b',
173 '\t': '\\t',
174 '\n': '\\n',
175 '\f': '\\f',
176 '\r': '\\r',
177 '"' : '\\"',
178 '\\': '\\\\'
179 };
180 })(jQuery);