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 ?
--
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/8087906e-6c16-4167-82ea-25a22665609dn%40googlegroups.com.
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
import java.util.HashMap;
import java.util.Map;
public class JacksonNaNMapValueExample
{
public static class SimpleClass
{
private Map<String, Double> map;
public SimpleClass() {}
public SimpleClass setMap(Map<String, Double> map)
{
this.map = map;
return this;
}
@Override public String toString()
{
return "SimpleClass{" + "map=" + map + '}';
}
}
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT). //
setSerializationInclusion(JsonInclude.Include.NON_NULL). //
disable(SerializationFeature.FAIL_ON_EMPTY_BEANS). //
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false). //
setVisibility(
mapper.getSerializationConfig() //
.getDefaultVisibilityChecker() //
.withFieldVisibility(JsonAutoDetect.Visibility.ANY) //
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)); //
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder().allowIfBaseType(Object.class).build();
mapper.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.NON_FINAL);
Map<String, Double> map = new HashMap<>();
map.put("A", 1.0d);
map.put("B", Double.NaN);
map.put("C", Double.POSITIVE_INFINITY);
SimpleClass classWithMapExample = new SimpleClass().setMap(map);
String jsonString = mapper.writeValueAsString(classWithMapExample);
System.out.println(String.format("Class with map example as json:\n%s", jsonString));
SimpleClass classWithMapExampleRestored = mapper.readValue(jsonString, SimpleClass.class);
System.out.println(String.format("Class with map example restored:\n%s", classWithMapExampleRestored));
}
}