3 * version: 2.1 (2009-08-14)
5 * This document is licensed as free software under the terms of the
6 * MIT License: http://www.opensource.org/licenses/mit-license.php
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
13 * It is also influenced heavily by MochiKit's serializeJSON, which is
14 * copyrighted 2005 by Bob Ippolito.
16 * @see http://code.google.com/p/jquery-json/
20 /** jQuery.toJSON( json-serializble )
21 Converts the given argument into a JSON respresentation.
23 If an object has a "toJSON" function, that will be used to get the representation.
24 Non-integer/string keys are skipped in the object, as are keys that point to a function.
27 The *thing* to be converted.
29 $.toJSON = function(o
)
31 if (typeof(JSON
) == 'object' && JSON
.stringify
)
32 return JSON
.stringify(o
);
39 if (type
== "undefined")
42 if (type
== "number" || type
== "boolean")
46 return $.quoteString(o
);
50 if (typeof o
.toJSON
== "function")
51 return $.toJSON( o
.toJSON() );
53 if (o
.constructor === Date
)
55 var month
= o
.getUTCMonth() + 1;
56 if (month
< 10) month
= '0' + month
;
58 var day
= o
.getUTCDate();
59 if (day
< 10) day
= '0' + day
;
61 var year
= o
.getUTCFullYear();
63 var hours
= o
.getUTCHours();
64 if (hours
< 10) hours
= '0' + hours
;
66 var minutes
= o
.getUTCMinutes();
67 if (minutes
< 10) minutes
= '0' + minutes
;
69 var seconds
= o
.getUTCSeconds();
70 if (seconds
< 10) seconds
= '0' + seconds
;
72 var milli
= o
.getUTCMilliseconds();
73 if (milli
< 100) milli
= '0' + milli
;
74 if (milli
< 10) milli
= '0' + milli
;
76 return '"' + year
+ '-' + month
+ '-' + day
+ 'T' +
77 hours
+ ':' + minutes
+ ':' + seconds
+
81 if (o
.constructor === Array
)
84 for (var i
= 0; i
< o
.length
; i
++)
85 ret
.push( $.toJSON(o
[i
]) || "null" );
87 return "[" + ret
.join(",") + "]";
97 else if (type
== "string")
98 name
= $.quoteString(k
);
100 continue; //skip non-string or number keys
102 if (typeof o
[k
] == "function")
103 continue; //skip pairs where the value is a function.
105 var val
= $.toJSON(o
[k
]);
107 pairs
.push(name
+ ":" + val
);
110 return "{" + pairs
.join(", ") + "}";
114 /** jQuery.evalJSON(src)
115 Evaluates a given piece of json source.
117 $.evalJSON = function(src
)
119 if (typeof(JSON
) == 'object' && JSON
.parse
)
120 return JSON
.parse(src
);
121 return eval("(" + src
+ ")");
124 /** jQuery.secureEvalJSON(src)
125 Evals JSON in a way that is *more* secure.
127 $.secureEvalJSON = function(src
)
129 if (typeof(JSON
) == 'object' && JSON
.parse
)
130 return JSON
.parse(src
);
133 filtered
= filtered
.replace(/\\["\\\/bfnrtu]/g, '@');
134 filtered
= filtered
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
135 filtered
= filtered
.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
137 if (/^[\],:{}\s]*$/.test(filtered
))
138 return eval("(" + src
+ ")");
140 throw new SyntaxError("Error parsing JSON, source is not valid.");
143 /** jQuery.quoteString(string)
144 Returns a string-repr of a string, escaping quotes intelligently.
145 Mostly a support function for toJSON.
148 >>> jQuery.quoteString("apple")
151 >>> jQuery.quoteString('"Where are we going?", she asked.')
152 "\"Where are we going?\", she asked."
154 $.quoteString = function(string
)
156 if (string
.match(_escapeable
))
158 return '"' + string
.replace(_escapeable
, function (a
)
161 if (typeof c
=== 'string') return c
;
163 return '\\u00' + Math
.floor(c
/ 16).toString(16) + (c
% 16).toString(16);
166 return '"' + string
+ '"';
169 var _escapeable
= /["\\\x00-\x1f\x7f-\x9f]/g;