Ticket #7817 (new enhancement)
Improve error handling in dojo.fromJson
| Reported by: | Matt Sgarlata | Owned by: | anonymous |
|---|---|---|---|
| Priority: | normal | Milestone: | tbd |
| Component: | General | Version: | 1.2beta |
| Severity: | normal | Keywords: | |
| Cc: | development-staff@… |
Description
There is no explicit error handling code in dojo.fromJson, so if you have some invalid JSON then you could be in for a nasty surprise. In FF I got "missing ) in parenthetical" and in IE I got "Line: 4, Error: Expected ')'". This can be very difficult to debug, and it would be immensely helpful if dojo could provide some help. Here is a revised version of the function with some explicit error handling. I am also including a test page where you can compare the current dojo behavior with the behavior of this improved function
dojo.fromJson = function(/*String*/ json){
// summary:
// Parses a [JSON](http://json.org) string to return a JavaScript object. Throws for invalid JSON strings.
// json:
// a string literal of a JSON item, for instance:
// `'{ "foo": [ "bar", 1, { "baz": "thud" } ] }'`
try {
return eval("(" + json + ")"); // Object
}
catch (e) {
console.warn("Invalid JSON: '" + json + "'");
throw Error("Invalid JSON: " + e.message);
}
}
Attachments
Change History
Note: See
TracTickets for help on using
tickets.