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?
The print(Object) method of PrintStream Class in Java is used to print the specified Object on the stream. This Object is taken as a parameter. Parameters: This method accepts a mandatory parameter object which is the Object to be printed in the Stream. Return Value: This method do not returns any value.
Example 1: Java program to print the object When we print the object, we can see that the output looks different. This is because while printing the object, the toString() method of the object class is called. It formats the object in the default format.
println() method to print object, it calls the toString() method of Object class.
You could use Gson to serialize to JSON as it now supports NaN and +/- infinity
More often than not ToStringBuilder.reflectionToString(Object) works great. It wont work when doing Maps though.
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