找不到类型[simple type,类java.lang.Class <java.lang.object>] </java.lang.object>的(Map)密钥反序列化器

时间:2014-11-25 05:48:40

标签: java jackson

我用杰克逊序列化Map<Class<?>, BaseClass>BaseClass有多个子类,每个子类都被杰克逊正确注释为序列化/反序列化。

  1. 是否可以调用ObjectMapper.readValue(file, Subclass.class)并获取Subclass实例?
  2. 如果是,我该如何避免此异常?
  3. 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)
    
    1. 杰克逊不应该知道如何对Class<?>进行反序列化,因为它是核心的JDK类型吗?
    2. 更新:我发现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

1 个答案:

答案 0 :(得分:1)

这是一个已知错误,应在2.5.0版本中修复:https://github.com/FasterXML/jackson-databind/issues/630