On Tue, Nov 23, 2021 at 10:35 AM Steffen Springer <[email protected]>
wrote:
> 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 ?
>
>
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 +-
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jackson-user/CAGrxA26XV-jetuz%2BjFe-kv4aeae06Ri6mFLw8KmDMaGwW-O_HQ%40mail.gmail.com.