使用默认类型</string,>时,在Map <string,object =“”>中反序列化自定义类型

时间:2013-07-26 12:42:12

标签: java json serialization jackson deserialization

所以我有一个简单的对象:

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

0 个答案:

没有答案