2 * jQuery JSON plugin 2.4.0
4 * @author Brantley Harris, 2009-2011
5 * @author Timo Tijhof, 2011-2012
6 * @source This plugin is heavily influenced by MochiKit's serializeJSON, which is
7 * copyrighted 2005 by Bob Ippolito.
8 * @source 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
12 * @license MIT License <http://www.opensource.org/licenses/mit-license.php>
17 var escape
= /["\\\x00-\x1f\x7f-\x9f]/g,
27 hasOwn
= Object
.prototype.hasOwnProperty
;
31 * Converts the given argument into a JSON representation.
33 * @param o {Mixed} The json-serializable *thing* to be converted
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
40 $.toJSON
= typeof JSON
=== 'object' && JSON
.stringify
? JSON
.stringify : function (o
) {
45 var pairs
, k
, name
, val
,
48 if (type
=== 'undefined') {
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.
58 if (type
=== 'number' || type
=== 'boolean') {
61 if (type
=== 'string') {
62 return $.quoteString(o
);
64 if (typeof o
.toJSON
=== 'function') {
65 return $.toJSON(o
.toJSON());
67 if (type
=== 'date') {
68 var month
= o
.getUTCMonth() + 1,
70 year
= o
.getUTCFullYear(),
71 hours
= o
.getUTCHours(),
72 minutes
= o
.getUTCMinutes(),
73 seconds
= o
.getUTCSeconds(),
74 milli
= o
.getUTCMilliseconds();
86 minutes
= '0' + minutes
;
89 seconds
= '0' + seconds
;
97 return '"' + year
+ '-' + month
+ '-' + day
+ 'T' +
98 hours
+ ':' + minutes
+ ':' + seconds
+
105 for (k
= 0; k
< o
.length
; k
++) {
106 pairs
.push($.toJSON(o
[k
]) || 'null');
108 return '[' + pairs
.join(',') + ']';
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') {
116 // Only include own properties,
117 // Filter out inherited prototypes
118 if (hasOwn
.call(o
, k
)) {
119 // Keys must be numerical or string. Skip others
121 if (type
=== 'number') {
122 name
= '"' + k
+ '"';
123 } else if (type
=== 'string') {
124 name
= $.quoteString(k
);
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
);
139 return '{' + pairs
.join(',') + '}';
145 * Evaluates a given json string.
147 * @param str {String}
149 $.evalJSON
= typeof JSON
=== 'object' && JSON
.parse
? JSON
.parse : function (str
) {
150 /*jshint evil: true */
151 return eval('(' + str
+ ')');
155 * jQuery.secureEvalJSON
156 * Evals JSON in a way that is *more* secure.
158 * @param str {String}
160 $.secureEvalJSON
= typeof JSON
=== 'object' && JSON
.parse
? JSON
.parse : function (str
) {
163 .replace(/\\["\\\/bfnrtu]/g, '@')
164 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
165 .replace(/(?:^|:|,)(?:\s*\[)+/g, '');
167 if (/^[\],:{}\s]*$/.test(filtered
)) {
168 /*jshint evil: true */
169 return eval('(' + str
+ ')');
171 throw new SyntaxError('Error parsing JSON, source is not valid.');
176 * Returns a string-repr of a string, escaping quotes intelligently.
177 * Mostly a support function for toJSON.
179 * >>> jQuery.quoteString('apple')
182 * >>> jQuery.quoteString('"Where are we going?", she asked.')
183 * "\"Where are we going?\", she asked."
185 $.quoteString = function (str
) {
186 if (str
.match(escape
)) {
187 return '"' + str
.replace(escape
, function (a
) {
189 if (typeof c
=== 'string') {
193 return '\\u00' + Math
.floor(c
/ 16).toString(16) + (c
% 16).toString(16);
196 return '"' + str
+ '"';