jquery.JSON.js (4617B)
1 /* 2 * This document is licensed as free software under the terms of the 3 * MIT License: <a href="http://www.opensource.org/licenses/mit-license.php" title="http://www.opensource.org/licenses/mit-license.php">http://www.opensource.org/licenses/mit-license.php</a> 4 * 5 * Adapted by Rahul Singla. 6 * 7 * Brantley Harris wrote this plugin. It is based somewhat on the JSON.org 8 * website's <a href="http://www.json.org/json2.js" title="http://www.json.org/json2.js">http://www.json.org/json2.js</a>, which proclaims: 9 * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that 10 * I uphold. 11 * 12 * It is also influenced heavily by MochiKit's serializeJSON, which is 13 * copyrighted 2005 by Bob Ippolito. 14 */ 15 16 /** 17 * jQuery.JSON.encode( json-serializble ) Converts the given argument into a 18 * JSON respresentation. 19 * 20 * If an object has a "toJSON" function, that will be used to get the 21 * representation. Non-integer/string keys are skipped in the object, as are 22 * keys that point to a function. 23 * 24 * json-serializble: The *thing* to be converted. 25 */ 26 jQuery.JSON = { 27 encode: function(o) { 28 if (typeof (JSON) == 'object' && JSON.stringify) 29 return JSON.stringify(o); 30 31 var type = typeof (o); 32 33 if (o === null) 34 return "null"; 35 36 if (type == "undefined") 37 return undefined; 38 39 if (type == "number" || type == "boolean") 40 return o + ""; 41 42 if (type == "string") 43 return this.quoteString(o); 44 45 if (type == 'object') { 46 if (typeof o.toJSON == "function") 47 return this.encode(o.toJSON()); 48 49 if (o.constructor === Date) { 50 var month = o.getUTCMonth() + 1; 51 if (month < 10) 52 month = '0' + month; 53 54 var day = o.getUTCDate(); 55 if (day < 10) 56 day = '0' + day; 57 58 var year = o.getUTCFullYear(); 59 60 var hours = o.getUTCHours(); 61 if (hours < 10) 62 hours = '0' + hours; 63 64 var minutes = o.getUTCMinutes(); 65 if (minutes < 10) 66 minutes = '0' + minutes; 67 68 var seconds = o.getUTCSeconds(); 69 if (seconds < 10) 70 seconds = '0' + seconds; 71 72 var milli = o.getUTCMilliseconds(); 73 if (milli < 100) 74 milli = '0' + milli; 75 if (milli < 10) 76 milli = '0' + milli; 77 78 return '"' + year + '-' + month + '-' + day + 'T' + hours + ':' 79 + minutes + ':' + seconds + '.' + milli + 'Z"'; 80 } 81 82 if (o.constructor === Array) { 83 var ret = []; 84 for ( var i = 0; i < o.length; i++) 85 ret.push(this.encode(o[i]) || "null"); 86 87 return "[" + ret.join(",") + "]"; 88 } 89 90 var pairs = []; 91 for ( var k in o) { 92 var name; 93 var type = typeof k; 94 95 if (type == "number") 96 name = '"' + k + '"'; 97 else if (type == "string") 98 name = this.quoteString(k); 99 else 100 continue; // skip non-string or number keys 101 102 if (typeof o[k] == "function") 103 continue; // skip pairs where the value is a function. 104 105 var val = this.encode(o[k]); 106 107 pairs.push(name + ":" + val); 108 } 109 110 return "{" + pairs.join(", ") + "}"; 111 } 112 }, 113 114 /** 115 * jQuery.JSON.decode(src) Evaluates a given piece of json source. 116 */ 117 decode: function(src) { 118 if (typeof (JSON) == 'object' && JSON.parse) 119 return JSON.parse(src); 120 return eval("(" + src + ")"); 121 }, 122 123 /** 124 * jQuery.JSON.decodeSecure(src) Evals JSON in a way that is *more* secure. 125 */ 126 decodeSecure: function(src) { 127 if (typeof (JSON) == 'object' && JSON.parse) 128 return JSON.parse(src); 129 130 var filtered = src; 131 filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@'); 132 filtered = filtered 133 .replace( 134 /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, 135 ']'); 136 filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, ''); 137 138 if (/^[\],:{}\s]*$/.test(filtered)) 139 return eval("(" + src + ")"); 140 else 141 throw new SyntaxError("Error parsing JSON, source is not valid."); 142 }, 143 144 /** 145 * jQuery.JSON.quoteString(string) Returns a string-repr of a string, escaping 146 * quotes intelligently. Mostly a support function for JSON.encode. 147 * 148 * Examples: >>> jQuery.JSON.quoteString("apple") "apple" 149 * 150 * >>> jQuery.JSON.quoteString('"Where are we going?", she asked.') "\"Where 151 * are we going?\", she asked." 152 */ 153 quoteString: function(string) { 154 if (string.match(this._escapeable)) { 155 return '"' + string.replace(this._escapeable, function(a) { 156 var c = this._meta[a]; 157 if (typeof c === 'string') 158 return c; 159 c = a.charCodeAt(); 160 return '\\u00' + Math.floor(c / 16).toString(16) 161 + (c % 16).toString(16); 162 }) + '"'; 163 } 164 return '"' + string + '"'; 165 }, 166 167 _escapeable: /["\\\x00-\x1f\x7f-\x9f]/g, 168 169 _meta: { 170 '\b': '\\b', 171 '\t': '\\t', 172 '\n': '\\n', 173 '\f': '\\f', 174 '\r': '\\r', 175 '"': '\\"', 176 '\\': '\\\\' 177 } 178 }