3 * version: 2.3 (2011-09-17)
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.
19 var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g,
32 * Converts the given argument into a JSON respresentation.
34 * @param o {Mixed} The json-serializble *thing* to be converted
36 * If an object has a toJSON prototype, that will be used to get the representation.
37 * Non-integer/string keys are skipped in the object, as are keys that point to a
41 $.toJSON = typeof JSON === 'object' && JSON.stringify
51 if ( type === 'undefined' ) {
54 if ( type === 'number' || type === 'boolean' ) {
57 if ( type === 'string') {
58 return $.quoteString( o );
60 if ( type === 'object' ) {
61 if ( typeof o.toJSON === 'function' ) {
62 return $.toJSON( o.toJSON() );
64 if ( o.constructor === Date ) {
65 var month = o.getUTCMonth() + 1,
67 year = o.getUTCFullYear(),
68 hours = o.getUTCHours(),
69 minutes = o.getUTCMinutes(),
70 seconds = o.getUTCSeconds(),
71 milli = o.getUTCMilliseconds();
83 minutes = '0' + minutes;
86 seconds = '0' + seconds;
94 return '"' + year + '-' + month + '-' + day + 'T' +
95 hours + ':' + minutes + ':' + seconds +
98 if ( o.constructor === Array ) {
100 for ( var i = 0; i < o.length; i++ ) {
101 ret.push( $.toJSON( o[i] ) || 'null' );
103 return '[' + ret.join(',') + ']';
110 if ( type === 'number' ) {
111 name = '"' + k + '"';
112 } else if (type === 'string') {
113 name = $.quoteString(k);
115 // Keys must be numerical or string. Skip others
120 if ( type === 'function' || type === 'undefined' ) {
121 // Invalid values like these return undefined
122 // from toJSON, however those object members
123 // shouldn't be included in the JSON string at all.
126 val = $.toJSON( o[k] );
127 pairs.push( name + ':' + val );
129 return '{' + pairs.join( ',' ) + '}';
135 * Evaluates a given piece of json source.
137 * @param src {String}
139 $.evalJSON = typeof JSON === 'object' && JSON.parse
142 return eval('(' + src + ')');
146 * jQuery.secureEvalJSON
147 * Evals JSON in a way that is *more* secure.
149 * @param src {String}
151 $.secureEvalJSON = typeof JSON === 'object' && JSON.parse
157 .replace( /\\["\\\/bfnrtu]/g, '@' )
158 .replace( /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
159 .replace( /(?:^|:|,)(?:\s*\[)+/g, '');
161 if ( /^[\],:{}\s]*$/.test( filtered ) ) {
162 return eval( '(' + src + ')' );
164 throw new SyntaxError( 'Error parsing JSON, source is not valid.' );
170 * Returns a string-repr of a string, escaping quotes intelligently.
171 * Mostly a support function for toJSON.
173 * >>> jQuery.quoteString('apple')
176 * >>> jQuery.quoteString('"Where are we going?", she asked.')
177 * "\"Where are we going?\", she asked."
179 $.quoteString = function( string ) {
180 if ( string.match( escapeable ) ) {
181 return '"' + string.replace( escapeable, function( a ) {
183 if ( typeof c === 'string' ) {
187 return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
190 return '"' + string + '"';