Can not find a (Map) Key deserializer for type [simple type, class com.comcast.ivr.core.domain.AutoHandlingSlotKey]

Mick Knutson picture Mick Knutson · Jun 16, 2011 · Viewed 49.5k times · Source

I have a domain object that has a Map:

private Map<AutoHandlingSlotKey, LinkedHashSet<AutoFunction>> autoHandling;

When I serialize the object, I get this:

"autoHandling" : [ "java.util.HashMap", {
} ],

This Map's key is a custom Object:

public class AutoHandlingSlotKey implements Serializable {
    private FunctionalArea slot; // ENUM
    private String returnView;   // ENUM

So, I am not sure how to correct this issue I keep getting when I deserialize the object:

org.codehaus.jackson.map.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class com.comcast.ivr.core.domain.AutoHandlingSlotKey]

Can someone help me to understand how to correct this issue if I do NOT have access to the domain object to modify?

Answer

estebanrv picture estebanrv · May 26, 2017

This was asked a long time ago, and is the first google result when looking up the error, but the accepted answer has no code and might be confusing for a jackson beginner (me). I eventually found this answer that helped.

So, as stated in accepted answer, Implementing and register a "key deserializer" is the way to go. You can do this like this.

SimpleModule simpleModule = new SimpleModule();
simpleModule.addKeyDeserializer(YourClass.class, new YourClassKeyDeserializer());
objectMapper.registerModule(simpleModule);

And for the class, all you have to do is:

class YourClassKeyDeserializer extends KeyDeserializer
{
    @Override
    public Object deserializeKey(final String key, final DeserializationContext ctxt ) throws IOException, JsonProcessingException
    {
        return null; // replace null with your logic
    }
}

That's it! No annotation on classes, not custom deserializer for maps, etc.