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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With