有关于Gson“循环参考”的解决方案吗? [英] Is there a solution about Gson "circular reference"?

查看:143
本文介绍了有关于Gson“循环参考”的解决方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



据我所知,一些解决方案是:

p>


  • 将导致循环引用的属性设置为transient。
  • 排除具有某些注释的属性。



但是作为一个普遍的问题,是否有一些共同的策略可以解决这个问题?

解决方案

据我所知,在Gson中没有针对循环引用的自动化解决方案。
我所知道的唯一能够自动处理循环引用的JSON生成库是 XStream (使用Jettison后端) 。


编辑:Jackson还支持使用 @JsonIdentityInfo 注释处理循环引用;所以虽然不是自动的(你确实需要标记需要对象标识处理的引用),但它确实可以解决大多数情况。


I have found many articles about the circular reference with Gson, but I can't find an elegant solution.

As I know, some solutions is:

  • Set the property that caused circular reference as "transient".
  • exclude the property with some annotation.

But as a general question, is there some common strategy to solve the problem?

解决方案

As far as I know there is no automated solution for circular references in Gson. The only JSON-producing library I know of that handles circular references automatically is XStream (with Jettison backend).

EDIT: Jackson also supports handling of circular references with @JsonIdentityInfo annotation; so while not automatic (you do need to mark references that need Object Id handling), it does allow solving most cases.

这篇关于有关于Gson“循环参考”的解决方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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