assemblyscript-json
assemblyscript-json copied to clipboard
How to catch JSON parse errors?
Is there a way to verify that a given input is indeed valid JSON?
I'm able to guard against the parsed value not being the object I expect, but not against it not being JSON at all:
let body = <JSON.Value>JSON.parse(text);
if (body != null && body.isObj) {
Good question. We should have better error handling. This is a larger issue with AS since there isn't really error handling just aborting. Perhaps JSON.parse should return an Result type.
An alternative with one less level of type wrapping would be to introduce a JSON.Error subtype of JSON.Value, this would not break backwards compatibility and all code that is already checking for the return type with isObj would work as before and could add an else if branch for the isError case.