commit | author | age
|
19351a
|
1 |
/* |
B |
2 |
* jQuery JSON Plugin |
|
3 |
* version: 1.0 (2008-04-17) |
|
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 technically wrote this plugin, but it is based somewhat |
|
9 |
* on the JSON.org 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. I really just cleaned it up. |
|
12 |
* |
|
13 |
* It is also based heavily on MochiKit's serializeJSON, which is |
|
14 |
* copywrited 2005 by Bob Ippolito. |
|
15 |
*/ |
|
16 |
|
|
17 |
(function($) { |
|
18 |
function toIntegersAtLease(n) |
|
19 |
// Format integers to have at least two digits. |
|
20 |
{ |
|
21 |
return n < 10 ? '0' + n : n; |
|
22 |
} |
|
23 |
|
|
24 |
Date.prototype.toJSON = function(date) |
|
25 |
// Yes, it polutes the Date namespace, but we'll allow it here, as |
|
26 |
// it's damned usefull. |
|
27 |
{ |
|
28 |
return this.getUTCFullYear() + '-' + |
|
29 |
toIntegersAtLease(this.getUTCMonth()) + '-' + |
|
30 |
toIntegersAtLease(this.getUTCDate()); |
|
31 |
}; |
|
32 |
|
|
33 |
var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g; |
|
34 |
var meta = { // table of character substitutions |
|
35 |
'\b': '\\b', |
|
36 |
'\t': '\\t', |
|
37 |
'\n': '\\n', |
|
38 |
'\f': '\\f', |
|
39 |
'\r': '\\r', |
|
40 |
'"' : '\\"', |
|
41 |
'\\': '\\\\' |
|
42 |
}; |
|
43 |
|
|
44 |
$.quoteString = function(string) |
|
45 |
// Places quotes around a string, inteligently. |
|
46 |
// If the string contains no control characters, no quote characters, and no |
|
47 |
// backslash characters, then we can safely slap some quotes around it. |
|
48 |
// Otherwise we must also replace the offending characters with safe escape |
|
49 |
// sequences. |
|
50 |
{ |
|
51 |
if (escapeable.test(string)) |
|
52 |
{ |
|
53 |
return '"' + string.replace(escapeable, function (a) |
|
54 |
{ |
|
55 |
var c = meta[a]; |
|
56 |
if (typeof c === 'string') { |
|
57 |
return c; |
|
58 |
} |
|
59 |
c = a.charCodeAt(); |
|
60 |
return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); |
|
61 |
}) + '"'; |
|
62 |
} |
|
63 |
return '"' + string + '"'; |
|
64 |
}; |
|
65 |
|
|
66 |
$.toJSON = function(o) |
|
67 |
{ |
|
68 |
var type = typeof(o); |
|
69 |
|
|
70 |
if (type == "undefined") |
|
71 |
return "undefined"; |
|
72 |
else if (type == "number" || type == "boolean") |
|
73 |
return o + ""; |
|
74 |
else if (o === null) |
|
75 |
return "null"; |
|
76 |
|
|
77 |
// Is it a string? |
|
78 |
if (type == "string") |
|
79 |
{ |
|
80 |
return $.quoteString(o); |
|
81 |
} |
|
82 |
|
|
83 |
// Does it have a .toJSON function? |
|
84 |
if (type == "object" && typeof o.toJSON == "function") |
|
85 |
return o.toJSON(); |
|
86 |
|
|
87 |
// Is it an array? |
|
88 |
if (type != "function" && typeof(o.length) == "number") |
|
89 |
{ |
|
90 |
var ret = []; |
|
91 |
for (var i = 0; i < o.length; i++) { |
|
92 |
ret.push( $.toJSON(o[i]) ); |
|
93 |
} |
|
94 |
return "[" + ret.join(",") + "]"; |
|
95 |
} |
|
96 |
|
|
97 |
// If it's a function, we have to warn somebody! |
|
98 |
if (type == "function") { |
|
99 |
throw new TypeError("Unable to convert object of type 'function' to json."); |
|
100 |
} |
|
101 |
|
|
102 |
// It's probably an object, then. |
|
103 |
var ret = []; |
|
104 |
for (var k in o) { |
|
105 |
var name; |
|
106 |
type = typeof(k); |
|
107 |
|
|
108 |
if (type == "number") |
|
109 |
name = '"' + k + '"'; |
|
110 |
else if (type == "string") |
|
111 |
name = $.quoteString(k); |
|
112 |
else |
|
113 |
continue; //skip non-string or number keys |
|
114 |
|
|
115 |
var val = $.toJSON(o[k]); |
|
116 |
if (typeof(val) != "string") { |
|
117 |
// skip non-serializable values |
|
118 |
continue; |
|
119 |
} |
|
120 |
|
|
121 |
ret.push(name + ":" + val); |
|
122 |
} |
|
123 |
return "{" + ret.join(",") + "}"; |
|
124 |
}; |
|
125 |
|
|
126 |
$.compactJSON = function(o) |
|
127 |
{ |
|
128 |
return $.toJSON(o, true); |
|
129 |
}; |
|
130 |
|
|
131 |
$.evalJSON = function(src) |
|
132 |
// Evals JSON that we know to be safe. |
|
133 |
{ |
|
134 |
return eval("(" + src + ")"); |
|
135 |
}; |
|
136 |
|
|
137 |
$.secureEvalJSON = function(src) |
|
138 |
// Evals JSON in a way that is *more* secure. |
|
139 |
{ |
|
140 |
var filtered = src; |
|
141 |
filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@'); |
|
142 |
filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'); |
|
143 |
filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, ''); |
|
144 |
|
|
145 |
if (/^[\],:{}\s]*$/.test(filtered)) |
|
146 |
return eval("(" + src + ")"); |
|
147 |
else |
|
148 |
throw new SyntaxError("Error parsing JSON, source is not valid."); |
|
149 |
}; |
|
150 |
})(jQuery); |