Data::Dumper for Java?

S

Sam

Hello,

if I have something like

whatever_class inst = new whatever_class();
inst.member_1 = value_1;
...
inst.member_n = value_n;

is there any possibility to find out (without explizit asking) what the
types and/or values from inst ?

I have to say that I'm new to JAVA and I was wondering if there is something
similar that I know from Perl with the module Data::Dumper.

e.g. in Perl:
Data::Dumper($inst) would result in something like that
{ member_1 => value_1,
.....
member_n => value_n
}

I doubt that there is something similar available (or even possible) but I
just wanted to ask.

Thanks,
Sam
 
T

Tim Tyler

Sam said:
I have to say that I'm new to JAVA and I was wondering if there is something
similar that I know from Perl with the module Data::Dumper.

e.g. in Perl:
Data::Dumper($inst) would result in something like that
{ member_1 => value_1,
....
member_n => value_n
}

I doubt that there is something similar available (or even possible) but I
just wanted to ask.

Sometimes objects are written in a way that toString() provides a textual
representation of the contents of the object.

Otherwise, it's a case of using reflection to access the data.
 
A

Alan Gutierrez

Hello,

if I have something like

whatever_class inst = new whatever_class();
inst.member_1 = value_1;
..
inst.member_n = value_n;

is there any possibility to find out (without explizit asking) what the
types and/or values from inst ?

I have to say that I'm new to JAVA and I was wondering if there is something
similar that I know from Perl with the module Data::Dumper.

e.g. in Perl:
Data::Dumper($inst) would result in something like that
{ member_1 => value_1,
....
member_n => value_n
}

I doubt that there is something similar available (or even possible) but I
just wanted to ask.

As mentioned elsewhere, you can use the facilities of the
java.lang.reflect package to determine the types and values of inst.

Reflection is probably what you want.

http://java.sun.com/docs/books/tutorial/reflect/

If for some reason, you really need the data to be in a string
format, then I recommend using the XStream library to serialize the
objects to XML, then you can use W3DOM, DOM4J, SAX, or XPath to poke
around a text repesentation of the document. Actually, I'd recommend
using DOM4J since it supports XPath and a Java friendly tree API.

http://xstream.codehaus.org/
http://www.dom4j.org/

(And try to use Java naming conventions, and I promise I'll use
underbars in my Perl code.)
 
Joined
Feb 11, 2011
Messages
1
Reaction score
0
Tim Tyler said:
Sometimes objects are written in a way that toString() provides a textual
representation of the contents of the object.

Otherwise, it's a case of using reflection to access the data.

I know I'm responding to an old post, but maybe this will be useful to some others who happen upon this thread.

It is true that some objects override the toString() method to return something descriptive and useful. In particular, the HashMap class (and most implementors of the Map interface) do this, printing their key/value pairs in a human-readable form. This works recursively, since toString() is also called on the keys and values, so for example HashMaps of HashMaps are displayed correctly.

The formatting is not as nice as Perl's Data::Dumper class though.

Note: I mention this because the OP's question specifically used the example of a Perl HASH, so if that is all that the OP is trying to do, this might be sufficient.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top