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

NPE in JSONObject.writeValue() #57

Closed
vvalchev opened this issue May 30, 2012 · 2 comments
Closed

NPE in JSONObject.writeValue() #57

vvalchev opened this issue May 30, 2012 · 2 comments

Comments

@vvalchev
Copy link

A NPE can occur if we try to serialize a JSONArray that contains a 'null' value. The NPE occurs in JSONObject @ line 1509, which is:

   } else if (value.getClass().isArray()) {

At this point 'value' can be null.

This can be fixed very easy. We just need to move the if statement @ line 1523 to be the first:

    } else if (value == null || value.equals(null)) {
        writer.write("null");
   }
@vvalchev
Copy link
Author

I was curious why an element in the JSONArray is real null, instead of the Null object defined in JSONObject, and the reason is that method of JSONArray

public JSONArray put(Object value) {
this.myArrayList.add(value);
return this;
}

Maybe the correct code is:
public JSONArray put(Object value) {
this.myArrayList.add(JSONObject.wrap(value));
return this;
}

@douglascrockford
Copy link
Contributor

Thanks. Please try it now.

With respect to JSON, Java's null is ambiguous. Sometimes it means NULL, and sometimes it means something like JavaScript's undefined.

BGehrels pushed a commit to BGehrels/JSON-java that referenced this issue Apr 29, 2020
test updates to make sure Enums are handled consistently.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants