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

parseJSONObject() and parseJSONArray() do not return null when parsing fails #165

Closed
denezth opened this issue Jan 21, 2021 · 2 comments
Closed

Comments

@denezth
Copy link

denezth commented Jan 21, 2021

Description

Both parseJSONObject() and parseJSONArray() throw a RuntimeException if the provided JSON String is not valid.

Expected Behavior

The documentation states:

If the String does not contain JSONObject data or cannot be parsed, a null value is returned.

Both functions should therefore return null in the event of a parsing error. This would not prevent the stackTrace from being displayed in console so that the user can understand the reason.

Steps to Reproduce

  1. Pass an invalid JSON String to either parseJSONObject() or parseJSONArray()
JSONObject object = parseJSONObject("invalidJSONString");

println(object);
  1. Run the sketch. A RuntimeException is thrown, stopping execution unless we use a try..catch.

My Environment

  • Processing version: 4.0a3
  • Operating System and OS version: MacOS 10.14.6

Possible Solution

In PApplet.java, we could catch the RuntimeException inside the parseJSONObject/Array(). Something like this:

public JSONObject parseJSONObject(String input) {
  try {
    return new JSONObject(new StringReader(input));
  } catch (RuntimeException e) {
    e.printStackTrace();
    return null;
  }
}

I will open a pull request.

@benfry
Copy link
Owner

benfry commented Jun 14, 2021

Resolved with 53246fb

Thanks again for your help!

@benfry benfry closed this as completed Jun 14, 2021
@github-actions
Copy link

This issue has been automatically locked. To avoid confusion with reports that have already been resolved, closed issues are automatically locked 30 days after the last comment. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants