使用抽象的类内容反序列化自定义集合

时间:2019-05-14 16:29:55

标签: java jackson json-deserialization

我有以下问题:

系统:

public class System {
    @JsonDeserialize(converter = MyDeserializer.class)
    ObservableList<AClass> aClassList;
}

AClass:

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "dataType")
@JsonSubTypes({
        @JsonSubTypes.Type(value = SubA.class),
        @JsonSubTypes.Type(value = SubB.class)
 })
public abstract class AClass{
}

SubA:

public class SubA extends AClass{
    public SubA(String name) {
    }
}

SubB:

public class SubB extends AClass{
    public SubB(String name) {
    }
}

MyDeserializer:

public class MyDeserializer implements Converter<List<AClass>, ObservableList<AClass>> {
    @Override
    public ObservableList<AClass> convert(List<AClass> celestialBodyList) {
        return FXCollections.observableList(celestialBodyList);
    }

    @Override
    public JavaType getInputType(TypeFactory typeFactory) {
        return typeFactory.constructCollectionType(List.class, AClass.class);
    }

    @Override
    public JavaType getOutputType(TypeFactory typeFactory) {
        return typeFactory.constructCollectionType(ObservableList.class, AClass.class);
    }
}

当我尝试反序列化System类时,出现以下错误:

com.fasterxml.jackson.databind.JsonMappingException: Cannot find a deserializer for non-concrete Collection type [collection type; class javafx.collections.ObservableList, contains [simple type, class AClass]]

您可以看到,即使我通过注释定义了解串器,杰克逊也无法找到反序列化器。

希望有人可以帮助我。

0 个答案:

没有答案