On Fri, Nov 2, 2018 at 2:19 PM Federico Fissore
<federico.fiss...@gmail.com> wrote:
>
> Hi all
>
> Given this code snippet
>
> String json = "{\"bar\": null}";
>
> JsonNode jsonNode = new ObjectMapper().readTree(json);
>
> System.out.println(jsonNode.get("bar").asText("banana 1"));
>
> System.out.println(jsonNode.get("foo").asText("banana 2"));
>
>
> it will print "banana 1" and then fail with a NullPointerException. Is this 
> expected? Should I create an issue or it's known and it's been discussed 
> already somewhere else?

Use method `path()` instead (or, if you want to create missing object,
`with()`); it works that way.

Semantics of `get()` are such that `null` is returned; and changing
that would break thousands of users everywhere (code that checks for
`null` to mean "isn't there"), so that is absolutely not going to
happen (for 2.x at least).
For 3.x it is possible to discuss naming, semantics, but it may not be
worth the hassle to change method names even there.

-+ Tatu +-

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to