Steffen Harbich
Steffen Harbich

Reputation: 2749

Jackson java.util.Date value in Map<String, Object> (de-)serialization

Consider this property

@JsonProperty
private Map<String, Object> myMap;

When a contained java.util.Date value is serialized as long, it will not be deserialized to Date again because the type information is not present in Map<String, Object>. How can I bypass the problem? I read answers about this question which would be a work around but there would be no way to distinguish strings containing dates from dates serialized as strings in the map. Can I tell Jackson to include type information for each map value such that Jackson can deserialize them correctly?

Upvotes: 7

Views: 2308

Answers (2)

Steffen Harbich
Steffen Harbich

Reputation: 2749

Finally, I came up with this solution. Deserializer:

private TypeReference<Map<String, Object>> typeRef = new TypeReference<Map<String, Object>>() {
};

@Override
public Map<String, Object> deserialize(JsonParser p, DeserializationContext ctxt, Map<String, Object> target) throws IOException {
    Map<String, Object> map = new ObjectMapper().readValue(p, typeRef);

    for (Map.Entry<String, Object> e : map.entrySet()) {
        if (e.getKey().endsWith("[date]")) {
            target.put(e.getKey().substring(0, e.getKey().length() - 6), new Date((Long) e.getValue()));
        }
        else {
            target.put(e.getKey(), e.getValue());
        }
    }

    return target;
}

Serializer:

@Override
public void serialize(Map<String, Object> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    Map<String, Object> adaptedValue = new HashMap<>(value);

    for (Map.Entry<String, Object> e : value.entrySet()) {
        if (e.getValue() instanceof Date) {
            adaptedValue.put(e.getKey() + "[date]", ((Date) e.getValue()).getTime());
            adaptedValue.remove(e.getKey());
        }
    }

    new ObjectMapper().writeValue(gen, adaptedValue);
}

The map key is adapted dependent on the data type. This is easily extendable.

Upvotes: 1

devops
devops

Reputation: 9179

Implement a custom Deserializer and add the Annotation @JsonDeserialize(using = DateDeserializer.class) to your field.

Take a look at this example:

Your Json-Bean:

public class Foo {

    private String            name;

    @JsonProperty
    @JsonDeserialize(using = DateDeserializer.class)
    private Map<String, Object> dates;

    [...] // getter, setter, equals, hashcode
}

Deserializer:

public class DateDeserializer extends JsonDeserializer<Map<String, Object>> {

    private TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {};

    @Override
    public Map<String, Object> deserialize(JsonParser p, DeserializationContext ctxt, Map<String, Object> target) throws IOException, JsonProcessingException {

        Map<String, Long> map = new ObjectMapper().readValue(p, typeRef);

        for(Entry<String, Long> e : map.entrySet()){

            Long value = e.getValue();
            String key = e.getKey();

            if(value instanceof Long){ // or if("date".equals(key)) ...
                target.put(key, new Date(value));
            } else {
                target.put(key, value); // leave as is
            }

        }

        return target;
    }

    @Override
    public Map<String, Object> deserialize(JsonParser paramJsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return this.deserialize(paramJsonParser, ctxt, new HashMap<>());
    }

}

Simple test:

public static void main(String[] args) throws Exception {

    Foo foo1 = new Foo();
    foo1.setName("foo");
    foo1.setData(new HashMap<String, Object>(){{
        put("date",   new Date());
        put("bool",   true);
        put("string", "yeah");
    }});
    ObjectMapper mapper = new ObjectMapper();
    String jsonStr = mapper.writeValueAsString(foo1);
    System.out.println(jsonStr);
    Foo foo2 = mapper.readValue(jsonStr, Foo.class);

    System.out.println(foo2.equals(foo1));

}

Upvotes: 5

Related Questions