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));
}