monkjack monkjack - 11 days ago 5x
Java Question

Custom Jackson Deserializer Getting Access to Current Field Class

I'm trying to write a custom deserializer for Jackson and I want to make it generic (generic in the sense of working on any type, not as in "generics").

However I cannot seem to figure out how to get a handle to the type of the field being deserialized.

Eg, I'm looking to do something like the following:

public MyObject deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {

Class c = <get type of current field>
// do something with that type
return new SubclassOfC(somedata based on c);

It's specifically the get type of current field part that I have struggled with.

Edit: It is the type of the java field I am interested in.


I have solved my particular problem by adding an implementation of Deserializers to the ObjectMapper. Eg

   Deserializers d = new Deserializers.Base() {

   public JsonDeserializer<?> findEnumDeserializer(Class<?> type, DeserializationConfig config, BeanDescription beanDesc, BeanProperty property)
                  throws JsonMappingException {
                if (property.getType().getContentType() != null)
                    return new EnumDeserializer(property.getType().getContentType().getRawClass());
                return new EnumDeserializer(property.getType().getRawClass());


This will return my custom EnumDeserializer instantiated for each separate Enum type.