Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Data::Dumper for Java?

Reply
Thread Tools

Data::Dumper for Java?

 
 
Sam
Guest
Posts: n/a
 
      11-30-2004
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:umper.

e.g. in Perl:
Data:umper($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


 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      11-30-2004
On Tue, 30 Nov 2004 15:05:41 GMT, Sam wrote:

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


Reflection..
<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getDeclaredFields()>

> I have to say that I'm new ..


A better group for now might be..
<http://www.physci.org/codes/javafaq.jsp#cljh>

>..to JAVA


It is 'Java', a proper name, rather than an acronym.

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Reply With Quote
 
Tim Tyler
Guest
Posts: n/a
 
      12-07-2004
Sam <> wrote or quoted:

> 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:umper.
>
> e.g. in Perl:
> Data:umper($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.
--
__________
|im |yler http://timtyler.org/ Remove lock to reply.
 
Reply With Quote
 
Alan Gutierrez
Guest
Posts: n/a
 
      12-08-2004
On 2004-11-30, Sam <> wrote:
> 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:umper.
>
> e.g. in Perl:
> Data:umper($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.)

--
Alan Gutierrez -
 
Reply With Quote
 
Anonymous Anonymous is offline
Junior Member
Join Date: Feb 2011
Posts: 1
 
      02-11-2011
Quote:
Originally Posted by Tim Tyler
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 by Anonymous; 02-11-2011 at 06:02 PM..
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Data::Dumper vs. UTF-8, as usual jidanni Perl Misc 5 03-23-2011 02:21 AM
Data::Dumper for java horos11@gmail.com Java 16 04-08-2009 11:30 AM
Data::Dumper and UTF-8 August Karlstrom Perl Misc 6 10-27-2007 01:42 PM
eval of Data::Dumper output not same as original data structure himanshu.garg@gmail.com Perl Misc 4 08-01-2007 01:49 PM
Data::Dumper and XML::Simple in Java rc Java 3 08-16-2004 09:51 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57