简体   繁体   中英

Writing custom deserializer for polymorphic type hierarchy Jackson

I'm experimenting with Jackson deserialization for inheritance in Java. I've a base class:

@Getter //Lombok @Getter
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes(value = {
        @JsonSubTypes.Type(value=ClassA.class, name = "classA")
})
public abstract class BaseClass {
   private List<String> fields;

   @JsonCreator
   public BaseClass(@JsonProperty("fields") final List<String> fields) {
     this.fields = fields;
   }
}

ClassA is also abstract

@Getter
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "typeA", include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes(value = {
        @JsonSubTypes.Type(value=SubClassA.class, name = "subclassA")
})
public abstract class ClassA extends BaseClass{
   private String mode;

   @JsonCreator
   public ClassA(@JsonProperty("fields") final List<String> fields, @JsonProperty("mode") String mode) {
       super(fields);
       this.mode = mode;
   }
}

My subClassA:

public class SubClassA extends ClassA {
   private String dummyField;

   public SubClassA(@JsonProperty("fields") final List<String> fields, @JsonProperty("mode") String mode,
     @JsonProperty("dummyField") String dummyField) {
       super(fields, mode);
       this.dummyField = dummyField;
   }

}

If I pass in a JSON of in the following form:

{
  "type": "classA",
  "typeA": "subclassA",
  "mode": "testingMode",
  "fields": ["1", "2"],
  "dummyField": "dummy"
}

I get an error Cannot construct instance of ClassA (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information I came across this https://github.com/FasterXML/jackson-databind/issues/374 which says this is a known issue with Jackson. How do I go about writing a customDeserializer for this.

In classA I tried doing this:

@JsonDeserialize(using = ClassADeserializer.class)

and ClassADeserializer is:

public class ClassADeserializer extends StdDeserializer<ClassA> {
    private final JsonDeserializer<?> defaultDeserializer;
    public ClassADeserializer(JsonDeserializer<?> defaultDeserializer) {
        super(ClassA.class);
        this.defaultDeserializer = defaultDeserializer;
    }

    @Override public ClassA deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
       return (ClassA) defaultDeserializer.deserialize(jsonParser, deserializationContext);
    }

which obviously doesn't work. How do I go about writing a custom deserializer for this?

Problem:

You pass in json "type": "classA", ... That means jackson first try to create instance of ClassA ..During deserialization jackson search @JsonCreator constructor first..If @JsonCreator missing or can not call @JsonCreator constructor then jackson create object with default constructor and call setter method... In your ClassA @JsonCreator constructor with 2 arguments but jackson call with 3 arguments.. So its fail. then jackson call default constructor to create instance. but default constructor also missing.. thats why u get this error: Cannot construct instance of ClassA (no Creators, like default construct, exist) ..

Solution:

As you want to deserialize to SubClassA ... You need to use @JsonCreator in SubClassA...Then you need to use @JsonIgnoreProperties to ignore properties type so that jackson create instance of SubClassA instead of ClassA ....

Try with below SubClassA :

@Getter
@JsonIgnoreProperties(ignoreUnknown = true)
public class SubClassA extends ClassA {
    private String dummyField;

    @JsonCreator
    public SubClassA(@JsonProperty("fields") final List<String> fields, @JsonProperty("mode") String mode,
                     @JsonProperty("dummyField") String dummyField) {
        super(fields, mode);
        this.dummyField = dummyField;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM