3 * version: 1.0 (2008-04-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 technically wrote this plugin, but it is based somewhat
9 * on the JSON.org website's http://www.json.org/json2.js, which proclaims:
10 * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that
11 * I uphold. I really just cleaned it up.
13 * It is also based heavily on MochiKit's serializeJSON, which is
14 * copywrited 2005 by Bob Ippolito.
18 function toIntegersAtLease(n
)
19 // Format integers to have at least two digits.
21 return n
< 10 ? '0' + n
: n
;
24 Date
.prototype.toJSON = function(date
)
25 // Yes, it polutes the Date namespace, but we'll allow it here, as
26 // it's damned usefull.
28 return this.getUTCFullYear() + '-' +
29 toIntegersAtLease(this.getUTCMonth()) + '-' +
30 toIntegersAtLease(this.getUTCDate());
33 var escapeable
= /["\\\x00-\x1f\x7f-\x9f]/g;
34 var meta
= { // table of character substitutions
44 $.quoteString = function(string
)
45 // Places quotes around a string, inteligently.
46 // If the string contains no control characters, no quote characters, and no
47 // backslash characters, then we can safely slap some quotes around it.
48 // Otherwise we must also replace the offending characters with safe escape
51 if (escapeable
.test(string
))
53 return '"' + string
.replace(escapeable
, function (a
)
56 if (typeof c
=== 'string') {
60 return '\\u00' + Math
.floor(c
/ 16).toString(16) + (c
% 16).toString(16);
63 return '"' + string
+ '"';
66 $.toJSON = function(o
, compact
)
70 if (type
== "undefined")
72 else if (type
== "number" || type
== "boolean")
80 return $.quoteString(o
);
83 // Does it have a .toJSON function?
84 if (type
== "object" && typeof o
.toJSON
== "function")
85 return o
.toJSON(compact
);
88 if (type
!= "function" && typeof(o
.length
) == "number")
91 for (var i
= 0; i
< o
.length
; i
++) {
92 ret
.push( $.toJSON(o
[i
], compact
) );
95 return "[" + ret
.join(",") + "]";
97 return "[" + ret
.join(", ") + "]";
100 // If it's a function, we have to warn somebody!
101 if (type
== "function") {
102 throw new TypeError("Unable to convert object of type 'function' to json.");
105 // It's probably an object, then.
111 if (type
== "number")
112 name
= '"' + k
+ '"';
113 else if (type
== "string")
114 name
= $.quoteString(k
);
116 continue; //skip non-string or number keys
118 var val
= $.toJSON(o
[k
], compact
);
119 if (typeof(val
) != "string") {
120 // skip non-serializable values
125 ret
.push(name
+ ":" + val
);
127 ret
.push(name
+ ": " + val
);
129 return "{" + ret
.join(", ") + "}";
132 $.compactJSON = function(o
)
134 return $.toJSON(o
, true);
137 $.evalJSON = function(src
)
138 // Evals JSON that we know to be safe.
140 return eval("(" + src
+ ")");
143 $.secureEvalJSON = function(src
)
144 // Evals JSON in a way that is *more* secure.
147 filtered
= filtered
.replace(/\\["\\\/bfnrtu]/g, '@');
148 filtered
= filtered
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
149 filtered
= filtered
.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
151 if (/^[\],:{}\s]*$/.test(filtered
))
152 return eval("(" + src
+ ")");
154 throw new SyntaxError("Error parsing JSON, source is not valid.");