|
12 | 12 | * @license MIT License <http://www.opensource.org/licenses/mit-license.php> |
13 | 13 | */ |
14 | 14 | /*global jQuery */ |
15 | | -/*jslint continue: true, plusplus: true */ |
| 15 | +/*jslint sloppy: true, continue: true, plusplus: true */ |
16 | 16 | (function ($) { |
17 | | - "use strict"; |
18 | | - |
19 | | - var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g, |
| 17 | + var escape = /["\\\x00-\x1f\x7f-\x9f]/g, |
20 | 18 | meta = { |
21 | 19 | '\b': '\\b', |
22 | 20 | '\t': '\\t', |
|
44 | 42 | return 'null'; |
45 | 43 | } |
46 | 44 |
|
47 | | - var pairs, i, k, name, val, |
48 | | - type = typeof o, |
49 | | - $type = $.type(o); |
| 45 | + var pairs, k, name, val, |
| 46 | + type = $.type(o); |
50 | 47 |
|
51 | 48 | if (type === 'undefined') { |
52 | 49 | return undefined; |
53 | 50 | } |
| 51 | + |
| 52 | + // Also covers instantiated Number and Boolean objects, |
| 53 | + // which are typeof 'object' but thanks to $.type, we |
| 54 | + // catch them here. I don't know whether it is right |
| 55 | + // or wrong that instantiated primitives are not |
| 56 | + // exported to JSON as an {"object":..}. |
| 57 | + // We choose this path because that's what the browsers did. |
54 | 58 | if (type === 'number' || type === 'boolean') { |
55 | 59 | return String(o); |
56 | 60 | } |
57 | 61 | if (type === 'string') { |
58 | 62 | return $.quoteString(o); |
59 | 63 | } |
60 | | - if (type === 'object') { |
61 | | - if (typeof o.toJSON === 'function') { |
62 | | - return $.toJSON(o.toJSON()); |
63 | | - } |
64 | | - if ($type === 'date') { |
65 | | - var month = o.getUTCMonth() + 1, |
66 | | - day = o.getUTCDate(), |
67 | | - year = o.getUTCFullYear(), |
68 | | - hours = o.getUTCHours(), |
69 | | - minutes = o.getUTCMinutes(), |
70 | | - seconds = o.getUTCSeconds(), |
71 | | - milli = o.getUTCMilliseconds(); |
| 64 | + if (typeof o.toJSON === 'function') { |
| 65 | + return $.toJSON(o.toJSON()); |
| 66 | + } |
| 67 | + if (type === 'date') { |
| 68 | + var month = o.getUTCMonth() + 1, |
| 69 | + day = o.getUTCDate(), |
| 70 | + year = o.getUTCFullYear(), |
| 71 | + hours = o.getUTCHours(), |
| 72 | + minutes = o.getUTCMinutes(), |
| 73 | + seconds = o.getUTCSeconds(), |
| 74 | + milli = o.getUTCMilliseconds(); |
72 | 75 |
|
73 | | - if (month < 10) { |
74 | | - month = '0' + month; |
75 | | - } |
76 | | - if (day < 10) { |
77 | | - day = '0' + day; |
78 | | - } |
79 | | - if (hours < 10) { |
80 | | - hours = '0' + hours; |
81 | | - } |
82 | | - if (minutes < 10) { |
83 | | - minutes = '0' + minutes; |
84 | | - } |
85 | | - if (seconds < 10) { |
86 | | - seconds = '0' + seconds; |
87 | | - } |
88 | | - if (milli < 100) { |
89 | | - milli = '0' + milli; |
90 | | - } |
91 | | - if (milli < 10) { |
92 | | - milli = '0' + milli; |
93 | | - } |
94 | | - return '"' + year + '-' + month + '-' + day + 'T' + |
95 | | - hours + ':' + minutes + ':' + seconds + |
96 | | - '.' + milli + 'Z"'; |
| 76 | + if (month < 10) { |
| 77 | + month = '0' + month; |
| 78 | + } |
| 79 | + if (day < 10) { |
| 80 | + day = '0' + day; |
97 | 81 | } |
| 82 | + if (hours < 10) { |
| 83 | + hours = '0' + hours; |
| 84 | + } |
| 85 | + if (minutes < 10) { |
| 86 | + minutes = '0' + minutes; |
| 87 | + } |
| 88 | + if (seconds < 10) { |
| 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 | + } |
98 | 101 |
|
99 | | - pairs = []; |
| 102 | + pairs = []; |
100 | 103 |
|
101 | | - if ($.isArray(o)) { |
102 | | - for (i = 0; i < o.length; i++) { |
103 | | - pairs.push($.toJSON(o[i]) || 'null'); |
104 | | - } |
105 | | - return '[' + pairs.join(',') + ']'; |
| 104 | + if ($.isArray(o)) { |
| 105 | + for (k = 0; k < o.length; k++) { |
| 106 | + pairs.push($.toJSON(o[k]) || 'null'); |
106 | 107 | } |
| 108 | + return '[' + pairs.join(',') + ']'; |
| 109 | + } |
107 | 110 |
|
108 | | - // Plain object |
| 111 | + // Any other object (plain object, RegExp, ..) |
| 112 | + // Need to do typeof instead of $.type, because we also |
| 113 | + // want to catch non-plain objects. |
| 114 | + if (typeof o === 'object') { |
109 | 115 | for (k in o) { |
110 | 116 | // Only include own properties, |
111 | 117 | // Filter out inherited prototypes |
112 | | - if (!hasOwn.call(o, k)) { |
113 | | - continue; |
114 | | - } |
115 | | - |
116 | | - // Keys must be numerical or string. Skip others |
117 | | - type = typeof k; |
118 | | - if (type === 'number') { |
119 | | - name = '"' + k + '"'; |
120 | | - } else if (type === 'string') { |
121 | | - name = $.quoteString(k); |
122 | | - } else { |
123 | | - continue; |
124 | | - } |
125 | | - type = typeof o[k]; |
| 118 | + if (hasOwn.call(o, k)) { |
| 119 | + // Keys must be numerical or string. Skip others |
| 120 | + type = typeof k; |
| 121 | + if (type === 'number') { |
| 122 | + name = '"' + k + '"'; |
| 123 | + } else if (type === 'string') { |
| 124 | + name = $.quoteString(k); |
| 125 | + } else { |
| 126 | + continue; |
| 127 | + } |
| 128 | + type = typeof o[k]; |
126 | 129 |
|
127 | | - // Invalid values like these return undefined |
128 | | - // from toJSON, however those object members |
129 | | - // shouldn't be included in the JSON string at all. |
130 | | - if (type === 'function' || type === 'undefined') { |
131 | | - continue; |
| 130 | + // Invalid values like these return undefined |
| 131 | + // from toJSON, however those object members |
| 132 | + // shouldn't be included in the JSON string at all. |
| 133 | + if (type !== 'function' && type !== 'undefined') { |
| 134 | + val = $.toJSON(o[k]); |
| 135 | + pairs.push(name + ':' + val); |
| 136 | + } |
132 | 137 | } |
133 | | - val = $.toJSON(o[k]); |
134 | | - pairs.push(name + ':' + val); |
135 | 138 | } |
136 | 139 | return '{' + pairs.join(',') + '}'; |
137 | 140 | } |
138 | 141 | }; |
139 | 142 |
|
140 | 143 | /** |
141 | 144 | * jQuery.evalJSON |
142 | | - * Evaluates a given piece of json source. |
| 145 | + * Evaluates a given json string. |
143 | 146 | * |
144 | | - * @param src {String} |
| 147 | + * @param str {String} |
145 | 148 | */ |
146 | | - $.evalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (src) { |
147 | | - return eval('(' + src + ')'); |
| 149 | + $.evalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (str) { |
| 150 | + return eval('(' + str + ')'); |
148 | 151 | }; |
149 | 152 |
|
150 | 153 | /** |
151 | 154 | * jQuery.secureEvalJSON |
152 | 155 | * Evals JSON in a way that is *more* secure. |
153 | 156 | * |
154 | | - * @param src {String} |
| 157 | + * @param str {String} |
155 | 158 | */ |
156 | | - $.secureEvalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (src) { |
| 159 | + $.secureEvalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (str) { |
157 | 160 | var filtered = |
158 | | - src |
| 161 | + str |
159 | 162 | .replace(/\\["\\\/bfnrtu]/g, '@') |
160 | 163 | .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') |
161 | 164 | .replace(/(?:^|:|,)(?:\s*\[)+/g, ''); |
162 | 165 |
|
163 | 166 | if (/^[\],:{}\s]*$/.test(filtered)) { |
164 | | - return eval('(' + src + ')'); |
| 167 | + return eval('(' + str + ')'); |
165 | 168 | } |
166 | 169 | throw new SyntaxError('Error parsing JSON, source is not valid.'); |
167 | 170 | }; |
|
177 | 180 | * >>> jQuery.quoteString('"Where are we going?", she asked.') |
178 | 181 | * "\"Where are we going?\", she asked." |
179 | 182 | */ |
180 | | - $.quoteString = function (string) { |
181 | | - if (string.match(escapeable)) { |
182 | | - return '"' + string.replace(escapeable, function (a) { |
| 183 | + $.quoteString = function (str) { |
| 184 | + if (str.match(escape)) { |
| 185 | + return '"' + str.replace(escape, function (a) { |
183 | 186 | var c = meta[a]; |
184 | 187 | if (typeof c === 'string') { |
185 | 188 | return c; |
|
188 | 191 | return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); |
189 | 192 | }) + '"'; |
190 | 193 | } |
191 | | - return '"' + string + '"'; |
| 194 | + return '"' + str + '"'; |
192 | 195 | }; |
193 | 196 |
|
194 | 197 | }(jQuery)); |
0 commit comments