我用杰克逊序列化Map<Class<?>, BaseClass>
。 BaseClass
有多个子类,每个子类都被杰克逊正确注释为序列化/反序列化。
ObjectMapper.readValue(file, Subclass.class)
并获取Subclass
实例?com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class java.lang.Class<java.lang.Object>]
at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:578)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findKeyDeserializer(DeserializerCache.java:168)
at com.fasterxml.jackson.databind.DeserializationContext.findKeyDeserializer(DeserializationContext.java:404)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.createContextual(MapDeserializer.java:231)
at com.fasterxml.jackson.databind.DeserializationContext.handleSecondaryContextualization(DeserializationContext.java:581)
at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:369)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:842)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:514)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:292)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:241)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:381)
at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3154)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3047)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2100)
Class<?>
进行反序列化,因为它是核心的JDK类型吗?更新:我发现https://stackoverflow.com/a/13954871/14731讨论了如何在杰克逊中实现多态性。这回答了问题#1但给我们留下了问题#2和#3。
UPDATE2 :有趣。我找到ClassSerializer.java,但https://github.com/FasterXML/jackson-databind/tree/master/src/main/java/com/fasterxml/jackson/databind/deser/std
中没有相应的ClassDeserializer.java
UPDATE3 :提交错误报告:https://github.com/FasterXML/jackson-databind/issues/630