'Deserialize class that implements Map as a bean

The Playlist class implements Map<String, Object>. Jackson deserializes a child object into a LinkedHashMap and then calls Map.put(). This throws an exception because the Map.put() implementation is trying to assign the LinkedHashMap to a field of another type. Jackson calls Map.put() because it is using a MapDeserializer. How do I change the deserializer to avoid calling Map.put()?

I figure if I tell Jackson to use a BeanDeserializer instead of a MapDeserializer, then everything will deserialize without a problem. Is there a better way?

Attempt #1

I created a JsonDeserializer and overrode deserialize() with this code:

return BeanDeserializerFactory.instance.
   createBeanDeserializer(context, m_type, m_description).
   deserialize(parser, context);

Unfortunately, this throws com.fasterxml.jackson.databind.exc.MismatchedInputException: No _valueDeserializer assigned. I am not sure how to assign _valueDeserializer.

Attempt #2

I then created a BeanDeserializerModifier and overrode modifyMapDeserializer() with this code:

DeserializationContext context;

if (!type.equals(m_type))
{
   return deserializer;
}
         
context = ??????;

try
{
   return BeanDeserializerFactory.instance.createBeanDeserializer(context, type, beanDesc);
}
catch (JsonMappingException caught)
{
   throw new UncheckedIOException(caught);
}

Unfortunately, I don't know how to get the DeserializationContext.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source