|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
(function($) { |
| 18 |
function toIntegersAtLease(n) |
| 19 |
|
| 20 |
{ |
| 21 |
return n < 10 ? '0' + n : n; |
| 22 |
} |
| 23 |
|
| 24 |
Date.prototype.toJSON = function(date) |
| 25 |
|
| 26 |
|
| 27 |
{ |
| 28 |
return this.getUTCFullYear() + '-' + |
| 29 |
toIntegersAtLease(this.getUTCMonth()) + '-' + |
| 30 |
toIntegersAtLease(this.getUTCDate()); |
| 31 |
}; |
| 32 |
|
| 33 |
var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g; |
| 34 |
var meta = { // table of character substitutions |
| 35 |
'\b': '\\b', |
| 36 |
'\t': '\\t', |
| 37 |
'\n': '\\n', |
| 38 |
'\f': '\\f', |
| 39 |
'\r': '\\r', |
| 40 |
'"' : '\\"', |
| 41 |
'\\': '\\\\' |
| 42 |
}; |
| 43 |
|
| 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 |
| 49 |
// sequences. |
| 50 |
{ |
| 51 |
if (escapeable.test(string)) |
| 52 |
{ |
| 53 |
return '"' + string.replace(escapeable, function (a) |
| 54 |
{ |
| 55 |
var c = meta[a]; |
| 56 |
if (typeof c === 'string') { |
| 57 |
return c; |
| 58 |
} |
| 59 |
c = a.charCodeAt(); |
| 60 |
return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); |
| 61 |
}) + '"'; |
| 62 |
} |
| 63 |
return '"' + string + '"'; |
| 64 |
}; |
| 65 |
|
| 66 |
$.toJSON = function(o, compact) |
| 67 |
{ |
| 68 |
var type = typeof(o); |
| 69 |
|
| 70 |
if (type == "undefined") |
| 71 |
return "undefined"; |
| 72 |
else if (type == "number" || type == "boolean") |
| 73 |
return o + ""; |
| 74 |
else if (o === null) |
| 75 |
return "null"; |
| 76 |
|
| 77 |
// Is it a string? |
| 78 |
if (type == "string") |
| 79 |
{ |
| 80 |
return $.quoteString(o); |
| 81 |
} |
| 82 |
|
| 83 |
// Does it have a .toJSON function? |
| 84 |
if (type == "object" && typeof o.toJSON == "function") |
| 85 |
return o.toJSON(compact); |
| 86 |
|
| 87 |
// Is it an array? |
| 88 |
if (type != "function" && typeof(o.length) == "number") |
| 89 |
{ |
| 90 |
var ret = []; |
| 91 |
for (var i = 0; i < o.length; i++) { |
| 92 |
ret.push( $.toJSON(o[i], compact) ); |
| 93 |
} |
| 94 |
if (compact) |
| 95 |
return "[" + ret.join(",") + "]"; |
| 96 |
else |
| 97 |
return "[" + ret.join(", ") + "]"; |
| 98 |
} |
| 99 |
|
| 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."); |
| 103 |
} |
| 104 |
|
| 105 |
// It's probably an object, then. |
| 106 |
var ret = []; |
| 107 |
for (var k in o) { |
| 108 |
var name; |
| 109 |
type = typeof(k); |
| 110 |
|
| 111 |
if (type == "number") |
| 112 |
name = '"' + k + '"'; |
| 113 |
else if (type == "string") |
| 114 |
name = $.quoteString(k); |
| 115 |
else |
| 116 |
continue; //skip non-string or number keys |
| 117 |
|
| 118 |
var val = $.toJSON(o[k], compact); |
| 119 |
if (typeof(val) != "string") { |
| 120 |
// skip non-serializable values |
| 121 |
continue; |
| 122 |
} |
| 123 |
|
| 124 |
if (compact) |
| 125 |
ret.push(name + ":" + val); |
| 126 |
else |
| 127 |
ret.push(name + ": " + val); |
| 128 |
} |
| 129 |
return "{" + ret.join(", ") + "}"; |
| 130 |
}; |
| 131 |
|
| 132 |
$.compactJSON = function(o) |
| 133 |
{ |
| 134 |
return $.toJSON(o, true); |
| 135 |
}; |
| 136 |
|
| 137 |
$.evalJSON = function(src) |
| 138 |
// Evals JSON that we know to be safe. |
| 139 |
{ |
| 140 |
return eval("(" + src + ")"); |
| 141 |
}; |
| 142 |
|
| 143 |
$.secureEvalJSON = function(src) |
| 144 |
// Evals JSON in a way that is *more* secure. |
| 145 |
{ |
| 146 |
var filtered = src; |
| 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, ''); |
| 150 |
|
| 151 |
if (/^[\],:{}\s]*$/.test(filtered)) |
| 152 |
return eval("(" + src + ")"); |
| 153 |
else |
| 154 |
throw new SyntaxError("Error parsing JSON, source is not valid."); |
| 155 |
}; |
| 156 |
})(jQuery); |