root / drupal7 / sites / all / modules / sweaver / plugins / sweaver_plugin_editor / jquery.json-2.2.js @ 651307cd
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 |
|
17 |
(function($) { |
18 |
/** jQuery.toJSON( json-serializble )
|
19 |
Converts the given argument into a JSON respresentation.
|
20 |
|
21 |
If an object has a "toJSON" function, that will be used to get the representation.
|
22 |
Non-integer/string keys are skipped in the object, as are keys that point to a function.
|
23 |
|
24 |
json-serializble:
|
25 |
The *thing* to be converted.
|
26 |
**/
|
27 |
$.toJSON = function(o) |
28 |
{ |
29 |
if (typeof(JSON) == 'object' && JSON.stringify) |
30 |
return JSON.stringify(o);
|
31 |
|
32 |
var type = typeof(o); |
33 |
|
34 |
if (o === null) |
35 |
return "null"; |
36 |
|
37 |
if (type == "undefined") |
38 |
return undefined; |
39 |
|
40 |
if (type == "number" || type == "boolean") |
41 |
return o + ""; |
42 |
|
43 |
if (type == "string") |
44 |
return $.quoteString(o); |
45 |
|
46 |
if (type == 'object') |
47 |
{ |
48 |
if (typeof o.toJSON == "function") |
49 |
return $.toJSON( o.toJSON() ); |
50 |
|
51 |
if (o.constructor === Date)
|
52 |
{ |
53 |
var month = o.getUTCMonth() + 1; |
54 |
if (month < 10) month = '0' + month; |
55 |
|
56 |
var day = o.getUTCDate();
|
57 |
if (day < 10) day = '0' + day; |
58 |
|
59 |
var year = o.getUTCFullYear();
|
60 |
|
61 |
var hours = o.getUTCHours();
|
62 |
if (hours < 10) hours = '0' + hours; |
63 |
|
64 |
var minutes = o.getUTCMinutes();
|
65 |
if (minutes < 10) minutes = '0' + minutes; |
66 |
|
67 |
var seconds = o.getUTCSeconds();
|
68 |
if (seconds < 10) seconds = '0' + seconds; |
69 |
|
70 |
var milli = o.getUTCMilliseconds();
|
71 |
if (milli < 100) milli = '0' + milli; |
72 |
if (milli < 10) milli = '0' + milli; |
73 |
|
74 |
return '"' + year + '-' + month + '-' + day + 'T' + |
75 |
hours + ':' + minutes + ':' + seconds + |
76 |
'.' + milli + 'Z"'; |
77 |
} |
78 |
|
79 |
if (o.constructor === Array)
|
80 |
{ |
81 |
var ret = [];
|
82 |
for (var i = 0; i < o.length; i++) |
83 |
ret.push( $.toJSON(o[i]) || "null" ); |
84 |
|
85 |
return "[" + ret.join(",") + "]"; |
86 |
} |
87 |
|
88 |
var pairs = [];
|
89 |
for (var k in o) { |
90 |
var name;
|
91 |
var type = typeof k; |
92 |
|
93 |
if (type == "number") |
94 |
name = '"' + k + '"'; |
95 |
else if (type == "string") |
96 |
name = $.quoteString(k);
|
97 |
else
|
98 |
continue; //skip non-string or number keys |
99 |
|
100 |
if (typeof o[k] == "function") |
101 |
continue; //skip pairs where the value is a function. |
102 |
|
103 |
var val = $.toJSON(o[k]); |
104 |
|
105 |
pairs.push(name + ":" + val);
|
106 |
} |
107 |
|
108 |
return "{" + pairs.join(", ") + "}"; |
109 |
} |
110 |
}; |
111 |
|
112 |
/** jQuery.evalJSON(src)
|
113 |
Evaluates a given piece of json source.
|
114 |
**/
|
115 |
$.evalJSON = function(src) |
116 |
{ |
117 |
if (typeof(JSON) == 'object' && JSON.parse) |
118 |
return JSON.parse(src);
|
119 |
return eval("(" + src + ")"); |
120 |
}; |
121 |
|
122 |
/** jQuery.secureEvalJSON(src)
|
123 |
Evals JSON in a way that is *more* secure.
|
124 |
**/
|
125 |
$.secureEvalJSON = function(src) |
126 |
{ |
127 |
if (typeof(JSON) == 'object' && JSON.parse) |
128 |
return JSON.parse(src);
|
129 |
|
130 |
var filtered = src;
|
131 |
filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@'); |
132 |
filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'); |
133 |
filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, ''); |
134 |
|
135 |
if (/^[\],:{}\s]*$/.test(filtered)) |
136 |
return eval("(" + src + ")"); |
137 |
else
|
138 |
throw new SyntaxError("Error parsing JSON, source is not valid."); |
139 |
}; |
140 |
|
141 |
/** jQuery.quoteString(string)
|
142 |
Returns a string-repr of a string, escaping quotes intelligently.
|
143 |
Mostly a support function for toJSON.
|
144 |
|
145 |
Examples:
|
146 |
>>> jQuery.quoteString("apple")
|
147 |
"apple"
|
148 |
|
149 |
>>> jQuery.quoteString('"Where are we going?", she asked.')
|
150 |
"\"Where are we going?\", she asked."
|
151 |
**/
|
152 |
$.quoteString = function(string) |
153 |
{ |
154 |
if (string.match(_escapeable))
|
155 |
{ |
156 |
return '"' + string.replace(_escapeable, function (a) |
157 |
{ |
158 |
var c = _meta[a];
|
159 |
if (typeof c === 'string') return c; |
160 |
c = a.charCodeAt(); |
161 |
return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); |
162 |
}) + '"';
|
163 |
} |
164 |
return '"' + string + '"'; |
165 |
}; |
166 |
|
167 |
var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g; |
168 |
|
169 |
var _meta = {
|
170 |
'\b': '\\b', |
171 |
'\t': '\\t', |
172 |
'\n': '\\n', |
173 |
'\f': '\\f', |
174 |
'\r': '\\r', |
175 |
'"' : '\\"', |
176 |
'\\': '\\\\' |
177 |
}; |
178 |
})(jQuery); |