Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Exception when serializing a JSONObject with null values #2

Closed
mpoindexter opened this issue Apr 9, 2012 · 1 comment
Closed

Exception when serializing a JSONObject with null values #2

mpoindexter opened this issue Apr 9, 2012 · 1 comment
Milestone

Comments

@mpoindexter
Copy link
Contributor

When attempting to serialize a JSONObject with a null value the following exception is thrown:

java.util.NoSuchElementException: JSONObject["xxxx"] not found.
at org.json.JSONObject.get(JSONObject.java:220)
at com.fasterxml.jackson.datatype.jsonorg.JSONObjectSerializer.serializeContents(JSONObjectSerializer.java:57)
at com.fasterxml.jackson.datatype.jsonorg.JSONObjectSerializer.serialize(JSONObjectSerializer.java:28)
at com.fasterxml.jackson.datatype.jsonorg.JSONObjectSerializer.serialize(JSONObjectSerializer.java:14)

It seems like the following code in JSONObjectSerializer

        try {
            ob = value.get(key);
        } catch (JSONException e) {
            throw new JsonGenerationException(e);
        }
        if (ob == null || ob == JSONObject.NULL) {
            if (provider.isEnabled(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES)) {
                jgen.writeNullField(key);
            }
            continue;
        }

should instead be:

        try {
            ob = value.opt(key);
        } catch (JSONException e) {
            throw new JsonGenerationException(e);
        }
        if (ob == null || ob == JSONObject.NULL) {
            if (provider.isEnabled(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES)) {
                jgen.writeNullField(key);
            }
            continue;
        }
cowtowncoder added a commit that referenced this issue Dec 27, 2013
@cowtowncoder
Copy link
Member

Ok. Fix is in, although I wasn't able to reproduce the original issue; but change makes sense regardless.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants