使用 Jackson 和 Autovalue 进行多态列表序列化

问题描述

我正在将 Autovalue 对象列表序列化为数据持有者,如下面的最小示例:

public class DataHolder {
    public List<Super> stuff = new ArrayList<>();
}

值如下所示:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes({
        @JsonSubTypes.Type(value = SubOne.class,name = "one")
})
interface Super{}
@Autovalue
public abstract class SubOne implements Super{
    @JsonCreator
    public static SubOne create() {
        return new Autovalue_SubOne();
    }
}

当我尝试按如下方式编组数据持有者时:

DataHolder holder = new DataHolder();
holder.stuff.add(SubOne.create());

ObjectMapper mapper = new ObjectMapper();

final byte[] bytes = mapper.writeValueAsBytes(holder);
final DataHolder list = mapper.readValue(bytes,DataHolder.class);

我收到以下异常:

Could not resolve type id 'Autovalue_SubOne' into a subtype of [simple type,class Super]
 at [Source: [B@4a94f1a5; line: 1,column: 12] (through reference chain: DataHolder["stuff"])
com.fasterxml.jackson.databind.JsonMappingException: Could not resolve type id 'Autovalue_SubOne' into a subtype of [simple type,column: 12] (through reference chain: DataHolder["stuff"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
    at com.fasterxml.jackson.databind.DeserializationContext.unkNownTypeException(DeserializationContext.java:677)
    at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._findDeserializer(TypeDeserializerBase.java:158)

是否有配置将生成的 Autovalue 对象映射回集合中的多态序列化的注释值?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)