Skip to content

Allow for reading null fields when reading simple objects #73

@jbartok

Description

@jbartok

Let's say I use JSON.std.beanFrom(MyBean.class, INPUT) as stated in the docs and MyBean has a Date field. If the JSON being parsed has a numeric value for that data field, then everything works ok, but if the value is null (for example "birthday":null), then I end up with: com.fasterxml.jackson.jr.ob.JSONObjectException: Can not get long numeric value from JSON (to construct java.util.Date) from 'null'.

This seems to be an arbitrary limitation caused by SimpleValueReader.read having following block:

case SER_DATE:
    return new Date(_fetchLong(p));
protected long _fetchLong(JsonParser p) throws IOException
    {
        JsonToken t = p.currentToken();
        if (t == JsonToken.VALUE_NUMBER_INT) {
            return p.getLongValue();
        }
        throw JSONObjectException.from(p, "Can not get long numeric value from JSON (to construct "
                +_valueType.getName()+") from "+_tokenDesc(p, t));
    }

Any reason to not improve on this, to return an actual null value instead of throwing an exception? Conceptually at least, seam the right thing to do, especially since reading into a Map (as opposed to POJO) works like that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions