Jackson custom deserializer for one field with polymorphic types

xi.lin picture xi.lin · Dec 8, 2014 · Viewed 31.1k times · Source

Update:

I tried to debug in jackson source code and find out that in the method

deserialize(JsonParser jp, DeserializationContext ctxt) of

SettableBeanProperty.java

when the _valueTypeDeserializer isn't null, it will never use _valueDeserializer.

Is this a correct assumption that TypeDeserializer should be more proper than ValueDeserializer?


I'm trying to use @JsonDeserialize to define custom deserializer for one field with polymorphic types. I can succeed to use @JsonDeserialize and @JsonTypeInfo seperately. But when i use them together, it seems that the @JsonDeserialize annotation is ignored.

Here is my class hierarchy:

1. Definition.java

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", defaultImpl = Definition.class)
@JsonSubTypes({@Type(value = Definition.class, name = "normal"),
    @Type(value = FormDefinition.class, name = "form")})
public class Definition {

    private String name;
    private String description;

    // getter&setter for name&description
}

2. FormDefinition.java

public class FormDefinition extends Definition {

    private String formName;
    @JsonDeserialize(using = DefinitionDeserializer.class)
    private Definition definition;

    public String getFormName() {
        return formName;
    }

    public void setFormName(String formName) {
        this.formName = formName;
    }

    public void setDefinition(Definition definition) {
        this.definition = definition;
    }
}

3. DefinitionDeserializer.java

public class DefinitionDeserializer extends JsonDeserializer<Definition> {

    @Override 
    public Definition deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
            throws IOException, JsonProcessingException {
        Definition definition = new Definition();
        String name = jsonParser.readValueAs(String.class);
        definition.setName(name);
        return definition;
    }
}

Sample main

public class App 
{
    public static void main( String[] args ) throws IOException {
        String sampleData = "{\"type\":\"form\",\"definition\":\"super\",\"formName\":\"FormName\"}";
        ObjectMapper mapper = new ObjectMapper();
        Definition definition = mapper.readValue(sampleData, Definition.class);
        System.out.println(definition);
    }
}

Running the sample main app will throw an exception:

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class jp.co.worksap.model.Definition] from String value ('super'); no single-String constructor/factory method (through reference chain: jp.co.worksap.model.FormDefinition["definition"])

which seems using AsPropertyTypeDeserializer to deserialize the definition field of FormDefinition class instead of the annotated deserializer DefinitionDeserializer.

I think the tricky part here is that the field I want to use custom deserializer is also type of Definition which using @JsonTypeInfo to solve the polymorphic problems.

Is there any way to use them together? Thanks.

Answer

Anton Koscejev picture Anton Koscejev · May 11, 2016

Jackson processes @JsonTypeInfo before choosing which Deserializer to use, probably because the choice of Deserializer could depend generally on the type. However, you can easily disable this on a per-field basis the same way you specify custom Deserializer - by annotating the field directly:

@JsonDeserialize(using = DefinitionDeserializer.class)
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
private Definition definition;