![](/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.