我想知道如何配置Jackson来反序列化“特殊”集合(不可变或所收集流的结果)中包含的对象的序列化列表。
这是我的问题,出于某些原因,我需要在jackson中声明de type,因为我需要能够反序列化所有内容并恢复所有类型。
public static class Sink {
private List items;
public Sink setItems(final List items) {
this.items = items;
return this;
}
public List getItems() {
return this.items;
}
}
public static void main(String[] args) throws Exception {
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.registerModule(new Jdk8Module());
mapper.registerModule(new AfterburnerModule());
mapper.enableDefaultTyping(NON_FINAL,JsonTypeInfo.As.PROPERTY);
String content = mapper.writeValueAsString(new Sink().setItems(List.of()));
System.out.println(content);
Sink result = mapper.readValue(content,Sink.class);
}
但是这会对集合产生不良影响。
content
的值是:
{
"@class": "api.Sink","items": ["java.util.ImmutableCollections$ListN",[]]
}
不幸的是,java.util.ImmutableCollections.*
没有默认的构造函数(有意义)。
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of
java.util.ImmutableCollections $ ListN (no Creators,like default construct,exist): no default no-arguments constructor found
我想知道如何迫使杰克逊将这类对象反序列化为ArrayList
或任何类型的列表。
我想将其配置为将列表的所有子类型反序列化为ArrayList
/ LinkedList
或在序列化以更改集合类型时
(java 12 / jackson 2.9.7)