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

Allow for reading null fields when reading simple objects #73

Closed
jbartok opened this issue Jul 7, 2020 · 4 comments
Closed

Allow for reading null fields when reading simple objects #73

jbartok opened this issue Jul 7, 2020 · 4 comments
Milestone

Comments

@jbartok
Copy link

jbartok commented Jul 7, 2020

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.

@cowtowncoder
Copy link
Member

cowtowncoder commented Jul 7, 2020

Just to make sure I understand: if POJO was like

public class Bean {
  public Date date;
}

and JSON

{ "date" : null }

then I think it absolutely should work so that null Date value would be used. Similarly, Long properties should accept null, and the only (?) open case would be if long (and other primitive values) should accept it.

It this the problem you have?

@cowtowncoder
Copy link
Member

I can reproduce the problem as described.

@cowtowncoder cowtowncoder removed the 2.11 label Jul 7, 2020
@cowtowncoder cowtowncoder added this to the 2.10.5 milestone Jul 7, 2020
@cowtowncoder
Copy link
Member

Fixed this specific problem for 2.10.5/2.11.2 (as well as related scalar types I saw).

@jbartok
Copy link
Author

jbartok commented Jul 8, 2020

Thank you, looking ahead for those versions to be released.

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