Java: Printing out an object for debugging

Muhd picture Muhd · Apr 14, 2011 · Viewed 23.9k times · Source

I would like an easy way to print out a java object, or to say it another way, serialize an object as a string. I would like to see the values of all variables contained within the object, and if there are more objects (like a list or whatever) it should print out the stuff in that object as well. Basically, it would be something like JSON encoding, but JSON doesn't support infinity, and I need to see if there is infinity or NaN in one of the double or float values.

How can I do this?

Answer

Daniel DiPaolo picture Daniel DiPaolo · Apr 14, 2011

You could use Gson to serialize to JSON as it now supports NaN and +/- infinity