所以我有一个简单的对象:
public class State {
private DateTime time;
private Map<String, Object> map;
....
}
我正在使用默认类型和自定义序列化器/反序列化器,ObjectMapper
如下所示:
SimpleModule module = new SimpleModule("Serialization", new Version(1, 0, 0, null))
.addSerializer(DateTime.class, new DateTimeSerializer())
.addDeserializer(DateTime.class, new DateTimeDeserializer());
ObjectMapper mapper = new ObjectMapper()
.registerModule(module)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.enableDefaultTyping(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT, JsonTypeInfo.As.WRAPPER_ARRAY);
该DateTime
字段直接位于对象中,使用我的序列化程序进行序列化。映射中的原始类型也可以正确序列化,在必要的地方利用包装器数组中的类型信息来保留类型。但是,如果我将自定义类型 (DateTime) 放入地图并尝试序列化,则会遇到以下问题:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Type id handling not implemented for type org.joda.time.DateTime (through reference chain: State["map"]->...HashMap["DateTime1"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:232)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:197)
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:184)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeTypedFields(MapSerializer.java:488)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:354)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeWithType(MapSerializer.java:334)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeWithType(MapSerializer.java:27)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:571)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:597)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:569)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:597)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:2718)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2210)
at com.rbs.agile.strategy.strategymanager.store.mongo.internal.JacksonDBObjectConverter.toDBObject(JacksonDBObjectConverter.java:26)
... 32 more
Caused by: java.lang.UnsupportedOperationException: Type id handling not implemented for type org.joda.time.DateTime
at com.fasterxml.jackson.databind.JsonSerializer.serializeWithType(JsonSerializer.java:142)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeTypedFields(MapSerializer.java:484)
... 45 more