Why does Jackson's treatment of Double.NaN differs depending on DefaultTyping ?

637 views
Skip to first unread message

Steffen Springer

unread,
Nov 23, 2021, 1:35:20 PM11/23/21
to jackson-user

Used version: jackson 2.13.0

In general writing and reading of Double.NaN works fine. However, if I want to write a class like this

public class SimpleClass { private Map<String, Double> map;} 

and the map contains a Double.NaN value, it gets tricky. 

With 

ObjectMapper.DefaultTyping.NON_FINAL)

it works, with

ObjectMapper.DefaultTyping.EVERYTHING)

a json is written, which cannot be read. Reading fails with an Exception

Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.lang.Double` from Array value (token `JsonToken.START_ARRAY`) at [Source: (String)"[ "de.epoq.rs.sai.utils.JacksonNaNMapValueExample$SimpleClass", { "map" : [ "java.util.HashMap", { "A" : 1.0, "B" : [ "java.lang.Double", "NaN" ], "C" : [ "java.lang.Double", "Infinity" ] } ] } ]"; line: 4, column: 11] (through reference chain: de.epoq.rs.sai.utils.JacksonNaNMapValueExample$SimpleClass["map"]->java.util.HashMap["B"]) at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1741) 

Please see the attached file for a full working example.


My question is:

Why is this happening ? Why is the map within the class is treated differently now ?

JacksonNaNMapValueExample.java

Tatu Saloranta

unread,
Nov 23, 2021, 3:07:18 PM11/23/21
to jackson-user
Hi Steffen! I would file an issue against jackson-databind, with a reproduction, ideally unit test.
It sounds like incorrect handling; I don't think it is intentional.

As to the cause this is probably related to the fact that Double is one of a small set of "natural" types (along
with String, Boolean and Long), for which additional Type Id is never added. This is an old and somewhat unfortunate
optimization, used to reduce Type Id overhead specifically for String values.
This probably leads to using a different deserializer in the Default Typing case or something which somehow does not
take into account NaNs.

With a reproduction it may be possible to avoid the issue.

-+ Tatu +-



 

Steffen Springer

unread,
Nov 24, 2021, 8:16:45 AM11/24/21
to jackson-user
Thank you very much Tatu for your response.

I did as suggested and filed a bug-report: https://github.com/FasterXML/jackson-databind/issues/3334

Reply all
Reply to author
Forward
0 new messages