Java object graph for JSON conversion with loops

How to convert Java object graph to JSON where the graph has cyclic dependencies / loops?

+4
source share
4 answers

There are many open source libraries that can generate JSON. Jersey can break circular dependencies. For others, you may need google.

+7
source

Note: this answer was written a long time ago. Use John, Jersey or Jackson.

I went with json-simple to my last project. It does not introduce any unnecessary project dependencies (such as apache-commons banks), and this is enough to properly parse / generate JSON.

You still have to manage your own circular links. I really doubt that there is such a library that is built for this. You can do this easily by adding to the Set all the objects you are converting, and then just checking to see if the object you are going to convert is in the set.

Also, I don't think json-simple will automatically serialize the object; those. You must submit the data you want to add to JSON. It just handles all the messy formatting for you.

+2
source

Not sure what you mean by circular dependencies, JSON is not able to express links. There is a lot of open source JSON lib to check the link http://x-stream.imtqy.com/json-tutorial.html to convert a java object to JSON

0
source

if you use quickxml. there is a new option and call @JsonIdentityInfo (generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")

. checkout for more information

just make sure your quickxml is in the correct version for this

0
source

Source: https://habr.com/ru/post/1336742/


All Articles