Jackson json 双向对象引用 [英] Jackson json two way object reference

查看:36
本文介绍了Jackson json 双向对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对像

public class Obj1 {
    public int id;
    public String name;
    public Obj2 obj2;
}

public class Obj2 {
    public int id;
    public String name;
    public List<Obj1> obj1list;
}

我希望能够通过 Jackson 将其转换为 Json.我找到了 JsonManagedReferenceJsonBackReference 并注释了它们,但是当你这样做时,序列化只能以一种方式工作.它只会在 JsonManagedReference 端的类被序列化时显示.

I want to be able to convert this to Json via Jackson. I found the JsonManagedReference and JsonBackReference and annotated them but when you do that, the serialization only works in one way. It will only show when the class with the JsonManagedReference side is serialized.

如果我序列化一个Obj1",我想看到附加到它的Obj2".如果我序列化Obj2",我想查看附加到它的Obj1"列表.

If I serialize an "Obj1" I want to see the "Obj2" that is attached to it. And if I serialize the "Obj2" I want to see the list of "Obj1"s that is attached to it.

我也试过像这样使用 JsonIdentityInfo 注释

I also tried using JsonIdentityInfo annotation like so

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")

这似乎有效,只是它将父对象的id"值添加到子对象(或列表案例中的每个子对象)中,这有点奇怪.虽然我想我可以忍受它.

and this seems to work except that it adds the "id" value of the parent object into the subobject (or every subobject in the list case) which is a bit odd. Though I guess I can live with it.

有没有办法让它按照我的预期运行?

Is there a way to get this to behave as I expect?

推荐答案

您正在查看 @JsonIgnoreProperties,它将给出所需的内容并避免 json 递归.

You are looking at @JsonIgnoreProperties, it will give what is needed and avoid json recursion.

public class Obj1 {
    public int id;
    public String name;
    @JsonIgnoreProperties("obj1list")
    public Obj2 obj2;
}

public class Obj2 {
    public int id;
    public String name;
    @JsonIgnoreProperties("obj2")
    public List<Obj1> obj1list;
}


更新

我总是喜欢 @JsonIgnoreProperties 而不是 JsonBackReferenceJsonManagedReference.因为它不仅可以解决问题,而且也不会逃避任何数据序列化(这是这里要求的情况).


UPDATE

I always perfers @JsonIgnoreProperties over JsonBackReference and JsonManagedReference. As it not only does the trick, but also not escapes any data serialization (which is the case required here).

我还有一个关于 github 的示例项目处理这种情况.查找实体类代码 School.java &District.java.如果你想运行一个测试,它是一个 mvn spring-boot 可执行代码.

I also have a sample project on github deals with this situation. Look for entity class codes School.java & District.java. Its an mvn spring-boot executable code, if you wanna run a test.

从 Javadoc 开始,从 Jackson 2.0 开始,@JsonIgnoreProperties 注释可以应用于类和属性.如果两者都使用,则实际集合将是所有忽略项的并集:也就是说,您只能添加要忽略的属性,而不能删除或覆盖.因此,您不能使用每个属性注释删除要忽略的属性.

From Javadoc, starting with Jackson 2.0, @JsonIgnoreProperties annotation can be applied both to classes and to properties. If used for both, actual set will be union of all ignorals: that is, you can only add properties to ignore, not remove or override. So you can not remove properties to ignore using per-property annotation.


工作原理

当您在属性级别定义 @JsonIgnoreProperties 时,在序列化/反序列化时它将在引用的属性对象上工作.

所以在这里,当解析 Obj1 时,您要求解析器忽略 obj2obj1list 属性.类似地,在解析 Obj2 时,它将忽略 Obj 集合中包含的 obj2 引用.因此,您的解析将如下所示:


HOW IT WORKS

When you define @JsonIgnoreProperties at propety level, while serialization/deserization it will work on the refered property object.

So here, when Obj1 is being parsed, you asked parser to ignore obj1list property of obj2. And similary, while parsing Obj2 it will ignore contained obj2 references in the Obj collection. So your parsing will look like below:

Obj1 : {
    id : int,
    name : string,
    Obj2 : {
     id : int,
     name : string
     obj1list : //ignored avoids recursion
    }
}

Obj2 : {
    id : int,
    name : string,
    obj1list : [{
     id : int,
     name : string,
     obj2 : //ignored avoids recursion
    },
    {
     id : int,
     name : string
     obj2 : //ignored avoids recursion
    }
    ]
}

这篇关于Jackson json 双向对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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