Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to catch JSON parse errors? #225

Open
trieloff opened this issue May 27, 2021 · 2 comments
Open

How to catch JSON parse errors? #225

trieloff opened this issue May 27, 2021 · 2 comments
Labels
question Further information is requested

Comments

@trieloff
Copy link

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) {
@willemneal willemneal added the question Further information is requested label Jul 1, 2021
@willemneal
Copy link
Contributor

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.

@trieloff
Copy link
Author

trieloff commented Jul 2, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants