3 Simple Json parser and Stringyfier, See: <http://www.json.org/>
11 Simple Json parser and Stringyfier, See: <http://www.json.org/>
18 Converts an object to a string, to be passed in server-side scripts as a parameter. Although its not normal usage for this class, this method can also be used to convert functions and arrays to strings.
21 obj - the object to convert to string
28 Json.toString({apple: 'red', lemon: 'yellow'}); '{"apple":"red","lemon":"yellow"}'
32 toString: function(obj){
35 return '"' + obj.replace(/(["\\])/g, '\\$1') + '"';
37 return '[' + obj.map(Json.toString).join(',') + ']';
40 for (var property in obj) string.push(Json.toString(property) + ':' + Json.toString(obj[property]));
41 return '{' + string.join(',') + '}';
48 converts a json string to an javascript Object.
51 str - the string to evaluate. if its not a string, it returns false.
52 secure - optionally, performs syntax check on json string. Defaults to false.
55 Json test regexp is by Douglas Crockford <http://crockford.org>.
58 >var myObject = Json.evaluate('{"apple":"red","lemon":"yellow"}');
59 >//myObject will become {apple: 'red', lemon: 'yellow'}
62 evaluate: function(str, secure){
63 return (($type(str) != 'string') || (secure && !str.test(/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/))) ? false : eval('(' + str + ')');