Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a solution about Gson "circular reference"?

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?

like image 528
Johnny Fee Avatar asked Dec 06 '11 15:12

Johnny Fee


1 Answers

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.

like image 81
StaxMan Avatar answered Sep 21 '22 14:09

StaxMan