I am having problems deserializing an object containing map with Class
as key:
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);
}
}
Throws this exception:
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)
...
When I am trying to serialize the map directly:
@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);
}
Then it works correctly. What am I missing in my first test?
EDIT:
Found out that the second test was incorrect. It should be:
@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>>(){});
}
Now it fails with same exception as the first one.
I submitted a finding on github . Tatu from FasterXML looked into it and fixed the issue. Problem was serialization of Class<?>
keys in map.
It should be fixed in jackson-databind version 2.5.1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.