I use Jackson library for serialization of my pojo objects into JSON representation. For example I have class A and class B:

class A {
  private int id;
  private B b;

  constructors...
  getters and setters
}

class B {
  private int ind;
  private A a;

  constructors...
  getters and setters
}

如果我想从类A中序列化对象,在序列化过程中有一定的可能得到递归.我知道我可以用@JsonIgnore来阻止它.

Is it possible to limit the serialization by depth-level ?

For example, if the level is 2, the serialization will go this way:

  • serialize a, level = 0 (0 < 2 ok) - > serialize
  • serialize a.b, level =1 (1 < 2 ok) -> serialize
  • serialize a.b.a, level = 2 (2 < 2 not true) -> stop

Thanks in advance.

推荐答案

I recently encountered a similar problem: Jackson - serialization of entities with birectional relationships (avoiding cycles)

因此,解决方案是升级到Jackson 2.0,并在类中添加以下注释:

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, 
                  property = "@id")
public class SomeEntityClass ...

This works perfectly.

Json相关问答推荐

在Go中,当字段可以根据其他字段具有不同的类型时,什么是正确的方法来卸载JSON?

过go 24小时内判断员事件的EventBridge事件模式

将 json 转换为 jsonb 安全吗?

jq可以在两个JSON对象列表中依次添加对象吗?

使用 jq 将消息转换为数组

JOLT 获取带有动态键的数组

Powershell中等效的JSONPath通配符以 Select 对象中的所有数组

从 json 数组中仅提取一个值导致 vb6

下一个 Js 部署在 getStaticPath 函数上失败:在 JSON.parse 的位置 0 的 JSON 中出现意外的令牌 < 但在本地运行

在 bash 中将 CSV 转换为 JSON

IE8 原生 JSON.parse 错误导致堆栈溢出

规范化 JSON 文件

如何通过 NSJSONSerialization 在 JSON 中包含空值?

jQuery fullcalendar 发送自定义参数并使用 JSON 刷新日历

如何向(JSON)对象的原型添加方法?

如何使用 Jackson 注释从 HttpResponse 反序列化 JSON 对象?

如何判断 JSON 响应元素是否为数组?

未调用 npm package.json 脚本

SCRIPT5009:JSON未定义

从动态 json 数据更新力有向图上的链接