杰克逊反序列化JsonIdentityReference(alwaysAsId = true) [英] Jackson deserialize JsonIdentityReference (alwaysAsId = true)

查看:559
本文介绍了杰克逊反序列化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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆