Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Ludevik opened this issue Jan 19, 2015 · 2 comments
Closed

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

Ludevik opened this issue Jan 19, 2015 · 2 comments

Comments

@Ludevik
Copy link

Ludevik commented Jan 19, 2015

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?

@cowtowncoder
Copy link
Member

Looks like problem is with serialization, not deserialization -- output is not correct, as it appears to use Class.toString(). So need to have a look at MapSerializers handling of key types.

@cowtowncoder
Copy link
Member

Added handling of Class<?> keys; unfortunately key serializers are separate from value serializers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants