Skip to content

Deserializing Map<Class<? extends Object>, String> #682

@Ludevik

Description

@Ludevik

I am having problems deserializing my Map<Class<? extends Object>, String>. Simple test case demonstrates it:

@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>>(){});
}

This test serializes the map as:

{
    "class java.util.ArrayList" : "ArrayList",
    "class java.util.HashMap" : "HashMap"
}

mapper.readValue(json, new TypeReference<Map<Class<? extends Object>, String>>(){}); then throws a 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: ...

As i understood from #630 the KeyDeserializer for Class should be part of Jackson. Am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions