Hello folks,

I've been trying to serialise and deserialise a Map<String, Object> whilst 
preserving the type of the elements, for example a Long should remain a 
Long and not become an Integer.
This works pretty well using the @JsonTypeInfo annotation on the Map. 
Jackson is being clever, and only including the type info when it is needed 
(e.g. it won't include it on an Integer, String, Double etc as they are 
default deserialisation types anyway), which is nice.
There's however a nasty edge case with Double.NaN, Double.POSITIVE_INFINITY 
and Double.NEGATIVE_INFINITY: they get serialised without type info, and 
get deserialised as Strings...

I've raised this as an issue 
<https://github.com/FasterXML/jackson-databind/issues/2236>, but in the 
meantime I'm looking for a workaround to include the type info on these 
Doubles (I've checked, and the deserialisation of NaN and infinity values 
works fine if the type is included).
Ideally, the customised code would only be invoked for that Map object, 
rather that for all serialisations (I've been able to override the 
DoubleSerializer altogether on the ObjectMapper, but would rather not make 
such an invasive change).

I've tried a few things, including with TypeIdResolver, but no luck so 
far... Any help would be much appreciated!

Here's a little repro

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Repro {
    private static ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) {
        try {
            String beanString = objectMapper.writeValueAsString(new Bean(1L, 
Double.NaN));
            MapHolder beanOut = objectMapper.readValue(beanString, 
MapHolder.class);
            System.out.println(beanOut.data.get("double").getClass());
            beanString = objectMapper.writeValueAsString(new Bean(1L, 1D));
            beanOut = objectMapper.readValue(beanString, MapHolder.class);
            System.out.println(beanOut.data.get("double").getClass());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static class Bean {
        private Long longValue;
        private Double doubleValue;

        public Bean(Long longValue, Double doubleValue) {
            this.longValue = longValue;
            this.doubleValue = doubleValue;
        }

        @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = 
JsonTypeInfo.As.PROPERTY, property = "@class")
        public Map<String, Object> getData() {
            Map<String, Object> map = new HashMap<>();
            map.put("long", longValue);
            map.put("double", doubleValue);
            return map;
        }
    }

    public static class MapHolder {
        public Map<String, Object> data;
        MapHolder() {}
    }
}



-- 
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 post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to