杰克逊反序列化JsonIdentityReference(alwaysAsId = true) [英] Jackson deserialize JsonIdentityReference (alwaysAsId = true)
问题描述
跟进此问题:此处提问
@JsonIdentityReference(alwaysAsId = true)
和
@JsonIdentityInfo(generator = ObjectIdGenerators。 PropertyGenerator.class)
在序列化结束时工作得很好,但是在反序列化时不太好,因为它无法解析对象ID引用。
@JsonIdentityReference(alwaysAsId = true)
and
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class)
works great from the Serialization end, but not so well when it comes time to deserialize since it can't resolve the Object ID reference.
有没有办法让它反序列化?编写自定义反序列化器似乎有点矫枉过正。
Is there a way to get this to deserialize? Writing a custom deserializer seems like overkill.
推荐答案
您可以使用简单的setter反序列化器来代替自定义反序列化器:
Instead of a custom deserializer, you can use a simple setter deserializer:
public class Container {
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonIdentityReference(alwaysAsId = true)
private Foo foo;
public Foo getFoo() {
return foo;
}
public Container setFoo(Foo foo) {
this.foo = foo;
return this;
}
@JsonProperty("foo")
public void setFoo(String id) {
foo = new Foo().setId(id);
}
}
{的示例字符串foo:id1}
在 Jackson 2.5.2中使用此方法正确序列化
这篇关于杰克逊反序列化JsonIdentityReference(alwaysAsId = true)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!