![](/img/trans.png)
[英]Can not find a (Map) Key deserializer for type [simple type, class java.lang.Class<java.lang.Object>]
[英]Can not construct Map key of type java.lang.Class from String: unable to parse key as Class
我在反序列化包含以Class
为键的map的对象时遇到问题:
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>>(){});
}
现在,它以与第一个相同的异常失败。
我在github上提交了一个发现。 FasterXML的Tatu对此进行了调查并解决了该问题。 问题是map中Class<?>
键的序列化。
它应该在杰克逊数据绑定版本2.5.1中修复
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.