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.
18 /** jQuery.toJSON( json-serializble )
19 Converts the given argument into a JSON respresentation.
21 If an object has a "toJSON" function, that will be used to get the representation.
22 Non-integer/string keys are skipped in the object, as are keys that point to a function.
25 The *thing* to be converted.
27 $.toJSON = function(o)
29 if (typeof(JSON) == 'object' && JSON.stringify)
30 return JSON.stringify(o);
37 if (type == "undefined")
40 if (type == "number" || type == "boolean")
44 return $.quoteString(o);
48 if (typeof o.toJSON == "function")
49 return $.toJSON( o.toJSON() );
51 if (o.constructor === Date)
53 var month = o.getUTCMonth() + 1;
54 if (month < 10) month = '0' + month;
56 var day = o.getUTCDate();
57 if (day < 10) day = '0' + day;
59 var year = o.getUTCFullYear();
61 var hours = o.getUTCHours();
62 if (hours < 10) hours = '0' + hours;
64 var minutes = o.getUTCMinutes();
65 if (minutes < 10) minutes = '0' + minutes;
67 var seconds = o.getUTCSeconds();
68 if (seconds < 10) seconds = '0' + seconds;
70 var milli = o.getUTCMilliseconds();
71 if (milli < 100) milli = '0' + milli;
72 if (milli < 10) milli = '0' + milli;
74 return '"' + year + '-' + month + '-' + day + 'T' +
75 hours + ':' + minutes + ':' + seconds +
79 if (o.constructor === Array)
82 for (var i = 0; i < o.length; i++)
83 ret.push( $.toJSON(o[i]) || "null" );
85 return "[" + ret.join(",") + "]";
95 else if (type == "string")
96 name = $.quoteString(k);
98 continue; //skip non-string or number keys
100 if (typeof o[k] == "function")
101 continue; //skip pairs where the value is a function.
103 var val = $.toJSON(o[k]);
105 pairs.push(name + ":" + val);
108 return "{" + pairs.join(", ") + "}";
112 /** jQuery.evalJSON(src)
113 Evaluates a given piece of json source.
115 $.evalJSON = function(src)
117 if (typeof(JSON) == 'object' && JSON.parse)
118 return JSON.parse(src);
119 return eval("(" + src + ")");
122 /** jQuery.secureEvalJSON(src)
123 Evals JSON in a way that is *more* secure.
125 $.secureEvalJSON = function(src)
127 if (typeof(JSON) == 'object' && JSON.parse)
128 return JSON.parse(src);
131 filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@');
132 filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
133 filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
135 if (/^[\],:{}\s]*$/.test(filtered))
136 return eval("(" + src + ")");
138 throw new SyntaxError("Error parsing JSON, source is not valid.");
141 /** jQuery.quoteString(string)
142 Returns a string-repr of a string, escaping quotes intelligently.
143 Mostly a support function for toJSON.
146 >>> jQuery.quoteString("apple")
149 >>> jQuery.quoteString('"Where are we going?", she asked.')
150 "\"Where are we going?\", she asked."
152 $.quoteString = function(string)
154 if (string.match(_escapeable))
156 return '"' + string.replace(_escapeable, function (a)
159 if (typeof c === 'string') return c;
161 return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
164 return '"' + string + '"';
167 var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;