|
Lines 1-5
Link Here
|
| 1 |
/** |
1 |
/** |
| 2 |
* jQuery JSON plugin 2.4-alpha |
2 |
* jQuery JSON plugin 2.4.0 |
| 3 |
* |
3 |
* |
| 4 |
* @author Brantley Harris, 2009-2011 |
4 |
* @author Brantley Harris, 2009-2011 |
| 5 |
* @author Timo Tijhof, 2011-2012 |
5 |
* @author Timo Tijhof, 2011-2012 |
|
Lines 12-199
Link Here
|
| 12 |
* @license MIT License <http://www.opensource.org/licenses/mit-license.php> |
12 |
* @license MIT License <http://www.opensource.org/licenses/mit-license.php> |
| 13 |
*/ |
13 |
*/ |
| 14 |
(function ($) { |
14 |
(function ($) { |
| 15 |
'use strict'; |
15 |
'use strict'; |
| 16 |
|
16 |
|
| 17 |
var escape = /["\\\x00-\x1f\x7f-\x9f]/g, |
17 |
var escape = /["\\\x00-\x1f\x7f-\x9f]/g, |
| 18 |
meta = { |
18 |
meta = { |
| 19 |
'\b': '\\b', |
19 |
'\b': '\\b', |
| 20 |
'\t': '\\t', |
20 |
'\t': '\\t', |
| 21 |
'\n': '\\n', |
21 |
'\n': '\\n', |
| 22 |
'\f': '\\f', |
22 |
'\f': '\\f', |
| 23 |
'\r': '\\r', |
23 |
'\r': '\\r', |
| 24 |
'"' : '\\"', |
24 |
'"' : '\\"', |
| 25 |
'\\': '\\\\' |
25 |
'\\': '\\\\' |
| 26 |
}, |
26 |
}, |
| 27 |
hasOwn = Object.prototype.hasOwnProperty; |
27 |
hasOwn = Object.prototype.hasOwnProperty; |
| 28 |
|
28 |
|
| 29 |
/** |
29 |
/** |
| 30 |
* jQuery.toJSON |
30 |
* jQuery.toJSON |
| 31 |
* Converts the given argument into a JSON representation. |
31 |
* Converts the given argument into a JSON representation. |
| 32 |
* |
32 |
* |
| 33 |
* @param o {Mixed} The json-serializable *thing* to be converted |
33 |
* @param o {Mixed} The json-serializable *thing* to be converted |
| 34 |
* |
34 |
* |
| 35 |
* If an object has a toJSON prototype, that will be used to get the representation. |
35 |
* If an object has a toJSON prototype, that will be used to get the representation. |
| 36 |
* Non-integer/string keys are skipped in the object, as are keys that point to a |
36 |
* Non-integer/string keys are skipped in the object, as are keys that point to a |
| 37 |
* function. |
37 |
* function. |
| 38 |
* |
38 |
* |
| 39 |
*/ |
39 |
*/ |
| 40 |
$.toJSON = typeof JSON === 'object' && JSON.stringify ? JSON.stringify : function (o) { |
40 |
$.toJSON = typeof JSON === 'object' && JSON.stringify ? JSON.stringify : function (o) { |
| 41 |
if (o === null) { |
41 |
if (o === null) { |
| 42 |
return 'null'; |
42 |
return 'null'; |
| 43 |
} |
43 |
} |
| 44 |
|
44 |
|
| 45 |
var pairs, k, name, val, |
45 |
var pairs, k, name, val, |
| 46 |
type = $.type(o); |
46 |
type = $.type(o); |
| 47 |
|
47 |
|
| 48 |
if (type === 'undefined') { |
48 |
if (type === 'undefined') { |
| 49 |
return undefined; |
49 |
return undefined; |
| 50 |
} |
50 |
} |
| 51 |
|
51 |
|
| 52 |
// Also covers instantiated Number and Boolean objects, |
52 |
// Also covers instantiated Number and Boolean objects, |
| 53 |
// which are typeof 'object' but thanks to $.type, we |
53 |
// which are typeof 'object' but thanks to $.type, we |
| 54 |
// catch them here. I don't know whether it is right |
54 |
// catch them here. I don't know whether it is right |
| 55 |
// or wrong that instantiated primitives are not |
55 |
// or wrong that instantiated primitives are not |
| 56 |
// exported to JSON as an {"object":..}. |
56 |
// exported to JSON as an {"object":..}. |
| 57 |
// We choose this path because that's what the browsers did. |
57 |
// We choose this path because that's what the browsers did. |
| 58 |
if (type === 'number' || type === 'boolean') { |
58 |
if (type === 'number' || type === 'boolean') { |
| 59 |
return String(o); |
59 |
return String(o); |
| 60 |
} |
60 |
} |
| 61 |
if (type === 'string') { |
61 |
if (type === 'string') { |
| 62 |
return $.quoteString(o); |
62 |
return $.quoteString(o); |
| 63 |
} |
63 |
} |
| 64 |
if (typeof o.toJSON === 'function') { |
64 |
if (typeof o.toJSON === 'function') { |
| 65 |
return $.toJSON(o.toJSON()); |
65 |
return $.toJSON(o.toJSON()); |
| 66 |
} |
66 |
} |
| 67 |
if (type === 'date') { |
67 |
if (type === 'date') { |
| 68 |
var month = o.getUTCMonth() + 1, |
68 |
var month = o.getUTCMonth() + 1, |
| 69 |
day = o.getUTCDate(), |
69 |
day = o.getUTCDate(), |
| 70 |
year = o.getUTCFullYear(), |
70 |
year = o.getUTCFullYear(), |
| 71 |
hours = o.getUTCHours(), |
71 |
hours = o.getUTCHours(), |
| 72 |
minutes = o.getUTCMinutes(), |
72 |
minutes = o.getUTCMinutes(), |
| 73 |
seconds = o.getUTCSeconds(), |
73 |
seconds = o.getUTCSeconds(), |
| 74 |
milli = o.getUTCMilliseconds(); |
74 |
milli = o.getUTCMilliseconds(); |
| 75 |
|
75 |
|
| 76 |
if (month < 10) { |
76 |
if (month < 10) { |
| 77 |
month = '0' + month; |
77 |
month = '0' + month; |
| 78 |
} |
78 |
} |
| 79 |
if (day < 10) { |
79 |
if (day < 10) { |
| 80 |
day = '0' + day; |
80 |
day = '0' + day; |
| 81 |
} |
81 |
} |
| 82 |
if (hours < 10) { |
82 |
if (hours < 10) { |
| 83 |
hours = '0' + hours; |
83 |
hours = '0' + hours; |
| 84 |
} |
84 |
} |
| 85 |
if (minutes < 10) { |
85 |
if (minutes < 10) { |
| 86 |
minutes = '0' + minutes; |
86 |
minutes = '0' + minutes; |
| 87 |
} |
87 |
} |
| 88 |
if (seconds < 10) { |
88 |
if (seconds < 10) { |
| 89 |
seconds = '0' + seconds; |
89 |
seconds = '0' + seconds; |
| 90 |
} |
|
|
| 91 |
if (milli < 100) { |
| 92 |
milli = '0' + milli; |
| 93 |
} |
| 94 |
if (milli < 10) { |
| 95 |
milli = '0' + milli; |
| 96 |
} |
| 97 |
return '"' + year + '-' + month + '-' + day + 'T' + |
| 98 |
hours + ':' + minutes + ':' + seconds + |
| 99 |
'.' + milli + 'Z"'; |
| 100 |
} |
90 |
} |
|
|
91 |
if (milli < 100) { |
| 92 |
milli = '0' + milli; |
| 93 |
} |
| 94 |
if (milli < 10) { |
| 95 |
milli = '0' + milli; |
| 96 |
} |
| 97 |
return '"' + year + '-' + month + '-' + day + 'T' + |
| 98 |
hours + ':' + minutes + ':' + seconds + |
| 99 |
'.' + milli + 'Z"'; |
| 100 |
} |
| 101 |
|
101 |
|
| 102 |
pairs = []; |
102 |
pairs = []; |
| 103 |
|
103 |
|
| 104 |
if ($.isArray(o)) { |
104 |
if ($.isArray(o)) { |
| 105 |
for (k = 0; k < o.length; k++) { |
105 |
for (k = 0; k < o.length; k++) { |
| 106 |
pairs.push($.toJSON(o[k]) || 'null'); |
106 |
pairs.push($.toJSON(o[k]) || 'null'); |
| 107 |
} |
|
|
| 108 |
return '[' + pairs.join(',') + ']'; |
| 109 |
} |
107 |
} |
|
|
108 |
return '[' + pairs.join(',') + ']'; |
| 109 |
} |
| 110 |
|
110 |
|
| 111 |
// Any other object (plain object, RegExp, ..) |
111 |
// Any other object (plain object, RegExp, ..) |
| 112 |
// Need to do typeof instead of $.type, because we also |
112 |
// Need to do typeof instead of $.type, because we also |
| 113 |
// want to catch non-plain objects. |
113 |
// want to catch non-plain objects. |
| 114 |
if (typeof o === 'object') { |
114 |
if (typeof o === 'object') { |
| 115 |
for (k in o) { |
115 |
for (k in o) { |
| 116 |
// Only include own properties, |
116 |
// Only include own properties, |
| 117 |
// Filter out inherited prototypes |
117 |
// Filter out inherited prototypes |
| 118 |
if (hasOwn.call(o, k)) { |
118 |
if (hasOwn.call(o, k)) { |
| 119 |
// Keys must be numerical or string. Skip others |
119 |
// Keys must be numerical or string. Skip others |
| 120 |
type = typeof k; |
120 |
type = typeof k; |
| 121 |
if (type === 'number') { |
121 |
if (type === 'number') { |
| 122 |
name = '"' + k + '"'; |
122 |
name = '"' + k + '"'; |
| 123 |
} else if (type === 'string') { |
123 |
} else if (type === 'string') { |
| 124 |
name = $.quoteString(k); |
124 |
name = $.quoteString(k); |
| 125 |
} else { |
125 |
} else { |
| 126 |
continue; |
126 |
continue; |
| 127 |
} |
127 |
} |
| 128 |
type = typeof o[k]; |
128 |
type = typeof o[k]; |
| 129 |
|
129 |
|
| 130 |
// Invalid values like these return undefined |
130 |
// Invalid values like these return undefined |
| 131 |
// from toJSON, however those object members |
131 |
// from toJSON, however those object members |
| 132 |
// shouldn't be included in the JSON string at all. |
132 |
// shouldn't be included in the JSON string at all. |
| 133 |
if (type !== 'function' && type !== 'undefined') { |
133 |
if (type !== 'function' && type !== 'undefined') { |
| 134 |
val = $.toJSON(o[k]); |
134 |
val = $.toJSON(o[k]); |
| 135 |
pairs.push(name + ':' + val); |
135 |
pairs.push(name + ':' + val); |
| 136 |
} |
136 |
} |
| 137 |
} |
137 |
} |
| 138 |
} |
|
|
| 139 |
return '{' + pairs.join(',') + '}'; |
| 140 |
} |
138 |
} |
| 141 |
}; |
139 |
return '{' + pairs.join(',') + '}'; |
|
|
140 |
} |
| 141 |
}; |
| 142 |
|
142 |
|
| 143 |
/** |
143 |
/** |
| 144 |
* jQuery.evalJSON |
144 |
* jQuery.evalJSON |
| 145 |
* Evaluates a given json string. |
145 |
* Evaluates a given json string. |
| 146 |
* |
146 |
* |
| 147 |
* @param str {String} |
147 |
* @param str {String} |
| 148 |
*/ |
148 |
*/ |
| 149 |
$.evalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (str) { |
149 |
$.evalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (str) { |
| 150 |
/*jshint evil: true */ |
150 |
/*jshint evil: true */ |
| 151 |
return eval('(' + str + ')'); |
151 |
return eval('(' + str + ')'); |
| 152 |
}; |
152 |
}; |
| 153 |
|
153 |
|
| 154 |
/** |
154 |
/** |
| 155 |
* jQuery.secureEvalJSON |
155 |
* jQuery.secureEvalJSON |
| 156 |
* Evals JSON in a way that is *more* secure. |
156 |
* Evals JSON in a way that is *more* secure. |
| 157 |
* |
157 |
* |
| 158 |
* @param str {String} |
158 |
* @param str {String} |
| 159 |
*/ |
159 |
*/ |
| 160 |
$.secureEvalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (str) { |
160 |
$.secureEvalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (str) { |
| 161 |
var filtered = |
161 |
var filtered = |
| 162 |
str |
162 |
str |
| 163 |
.replace(/\\["\\\/bfnrtu]/g, '@') |
163 |
.replace(/\\["\\\/bfnrtu]/g, '@') |
| 164 |
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') |
164 |
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') |
| 165 |
.replace(/(?:^|:|,)(?:\s*\[)+/g, ''); |
165 |
.replace(/(?:^|:|,)(?:\s*\[)+/g, ''); |
| 166 |
|
166 |
|
| 167 |
if (/^[\],:{}\s]*$/.test(filtered)) { |
167 |
if (/^[\],:{}\s]*$/.test(filtered)) { |
| 168 |
/*jshint evil: true */ |
168 |
/*jshint evil: true */ |
| 169 |
return eval('(' + str + ')'); |
169 |
return eval('(' + str + ')'); |
| 170 |
} |
170 |
} |
| 171 |
throw new SyntaxError('Error parsing JSON, source is not valid.'); |
171 |
throw new SyntaxError('Error parsing JSON, source is not valid.'); |
| 172 |
}; |
172 |
}; |
| 173 |
|
173 |
|
| 174 |
/** |
174 |
/** |
| 175 |
* jQuery.quoteString |
175 |
* jQuery.quoteString |
| 176 |
* Returns a string-repr of a string, escaping quotes intelligently. |
176 |
* Returns a string-repr of a string, escaping quotes intelligently. |
| 177 |
* Mostly a support function for toJSON. |
177 |
* Mostly a support function for toJSON. |
| 178 |
* Examples: |
178 |
* Examples: |
| 179 |
* >>> jQuery.quoteString('apple') |
179 |
* >>> jQuery.quoteString('apple') |
| 180 |
* "apple" |
180 |
* "apple" |
| 181 |
* |
181 |
* |
| 182 |
* >>> jQuery.quoteString('"Where are we going?", she asked.') |
182 |
* >>> jQuery.quoteString('"Where are we going?", she asked.') |
| 183 |
* "\"Where are we going?\", she asked." |
183 |
* "\"Where are we going?\", she asked." |
| 184 |
*/ |
184 |
*/ |
| 185 |
$.quoteString = function (str) { |
185 |
$.quoteString = function (str) { |
| 186 |
if (str.match(escape)) { |
186 |
if (str.match(escape)) { |
| 187 |
return '"' + str.replace(escape, function (a) { |
187 |
return '"' + str.replace(escape, function (a) { |
| 188 |
var c = meta[a]; |
188 |
var c = meta[a]; |
| 189 |
if (typeof c === 'string') { |
189 |
if (typeof c === 'string') { |
| 190 |
return c; |
190 |
return c; |
| 191 |
} |
191 |
} |
| 192 |
c = a.charCodeAt(); |
192 |
c = a.charCodeAt(); |
| 193 |
return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); |
193 |
return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); |
| 194 |
}) + '"'; |
194 |
}) + '"'; |
| 195 |
} |
195 |
} |
| 196 |
return '"' + str + '"'; |
196 |
return '"' + str + '"'; |
| 197 |
}; |
197 |
}; |
| 198 |
|
198 |
|
| 199 |
}(jQuery)); |
199 |
}(jQuery)); |
| 200 |
- |
|
|