I'm trying to write some code that will take any object and convert it into a String representation of that object. Serialization will not include any transient properties and toString() is typically a memory address for the object. I can't expect the object to have a meaningful toString() defined or implement Serializable.
Is there any way to determine the data in an object and convert it to a String? I would be willing to write something to traverse through the class and use reflection to find data, if need be, but I was hoping to find something a little simpler.
Any suggestions would be appreciated. Thanks.
Grab apache's commonns-lang. It has a ReflectionToStringBuilder
that will do the reflection to stringing for you.
It also accepts Styles to allow you to customize the output.
XStream
is another option for producing XML from your objects.
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