无法从String构造java.lang.Class类型的Map键:无法将键解析为Class

时间:2015-01-18 21:00:21

标签: java jackson json-deserialization

我在将包含Class的地图的对象反序列化时遇到问题:

public class TestSerializeMap {

    public static class TestClass {
        private Map<Class<? extends Object>, String> map = new HashMap<>();

        public TestClass() {
        }

        public Map<Class<? extends Object>, String> getMap() {
            return map;
        }
    }

    @Test
    public void testPropertyMapWithClassAsKey() throws Exception {
        TestClass testClass = new TestClass();
        testClass.getMap().put(ArrayList.class, "ArrayList");
        testClass.getMap().put(HashMap.class, "HashMap");

        ObjectMapper mapper = new ObjectMapper();

        String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(testClass);
        System.out.println(json);
        mapper.readValue(json, TestClass.class);
    }
}

引发此异常:

com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct     Map key of type java.lang.Class from String "class java.util.ArrayList": not a valid representation: Can not construct Map key of type java.lang.Class from String "class java.util.ArrayList": unable to parse key as Class
 at [Source: {
  "map" : {
    "class java.util.ArrayList" : "ArrayList",
    "class java.util.HashMap" : "HashMap"
  }
}; line: 3, column: 5]
 at [Source: {
  "map" : {
    "class java.util.ArrayList" : "ArrayList",
    "class java.util.HashMap" : "HashMap"
  }
}; line: 3, column: 5] (through reference chain: org.test.TestClass["map"])
    at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:55)
    at com.fasterxml.jackson.databind.DeserializationContext.weirdKeyException(DeserializationContext.java:913)
    at com.fasterxml.jackson.databind.deser.std.StdKeyDeserializer.deserializeKey(StdKeyDeserializer.java:131)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBind(MapDeserializer.java:404)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:333)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:25)
    ...

当我尝试直接序列化地图时:

@Test
public void testMapWithClassAsKey() throws Exception {
    Map<Class<? extends Object>, String> map = new HashMap<>();
    map.put(ArrayList.class, "ArrayList");
    map.put(HashMap.class, "HashMap");

    ObjectMapper mapper = new ObjectMapper();

    String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);
    System.out.println(json);
    mapper.readValue(json, Map.class);
}

然后它正常工作。 我在第一次测试中缺少什么?

编辑:

发现第二次测试不正确。它应该是:

@Test
public void testMapWithClassAsKey() throws Exception {
    Map<Class<? extends Object>, String> map = new HashMap<>();
    map.put(ArrayList.class, "ArrayList");
    map.put(HashMap.class, "HashMap");

    ObjectMapper mapper = new ObjectMapper();

    String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);
    System.out.println(json);
    mapper.readValue(json, new TypeReference<Map<Class<? extends Object>, String>>(){});
}

现在它失败了,与第一个异常相同。

1 个答案:

答案 0 :(得分:0)

我在github上提交了一份调查结果。来自FasterXML的Tatu调查了它并解决了这个问题。问题是地图中Class<?>键的序列化。

应该在jackson-databind版本2.5.1

中修复