How to convert a Java object graph to JSON, where the graph has circular dependencies / cycles?
There are many open source libraries that can generate JSON. Jersey can break the circular dependencies. For others you might want to google.
Note: this answer was written a long time ago. Use Gson, Jersey or Jackson.
I went with json-simple on my last project. It doesn't bring in any unneeded project dependencies (such as apache-commons jars) and was enough to parse/generate JSON correctly.
You'll still have to manage circular references yourself. I really doubt there is such a library that is built to handle this. You can do this easily by added to a Set any objects that you convert, and then just checking to see if the object you're about to convert is in the set.
Also, I don't think json-simple automatically serializes an object; that is, you have to feed it the data you want added to the JSON. It just handles all of the messy formatting for you.
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