Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Data::Dumper for java

 
 
horos11@gmail.com
Guest
Posts: n/a
 
      04-06-2009
Perl has a very nice class called Data:umper which recursively
prints out data structures and objects, such that if an object owns
other objects, those too are printed out in 'standardized' form (ie:
showing all public and private fields of those subobjects).

Is there an equivalent module in java?

I know you could make one using reflection; my question is *has*
someone made one, one that is tunable (ie: that can override objects'
toString() methods, shows only so many levels of output, etc. Being
able to override toString() for subobjects is very important because
Java so helpfully provides a somewhat meaningless toString() function
for Object)

Thanks much for any info,

Ed
 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      04-06-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Perl has a very nice class called Data:umper which recursively
> prints out data structures and objects, such that if an object owns
> other objects, those too are printed out in 'standardized' form (ie:
> showing all public and private fields of those subobjects).
>
> Is there an equivalent module in java?
>
> I know you could make one using reflection; my question is *has*
> someone made one, one that is tunable (ie: that can override objects'
> toString() methods, shows only so many levels of output, etc. Being
> able to override toString() for subobjects is very important because
> Java so helpfully provides a somewhat meaningless toString() function
> for Object)



I'm unfamiliar with Perl and it's libraries, so I have to ask -- what
would you do with such a thing?

For debugging, my debugger shows me all I need, with out any work
whatsoever on my part. I haven't looked into logging utilities but the
default ones do take an object as a parameter. This might just invoke
toString() however. Then there's Serialization and XMLEncoder/Decoder,
which might be consdiered "stadardized" forms also.

For documentation, there's this thing called Javadoc. Try it, you might
like it.
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      04-06-2009
(E-Mail Removed) writes:
>Is there an equivalent module in java?


Partially, there is no need for :ata:umper:umper, because
Java already has the, er, message »toString()« for this purpose.

But, »toString()« has difficulties. To get a meaningful dump,
one does not use »array.toString()«, but
»java.util.Arrays.toString( a )«.

»toString()« also might not always dump all details.

Objects can be dumped to XML using

http://xstream.codehaus.org/

.

I have started to writte my own dumper, which still is
experimental, incomplete work-in-progress with some known bugs
and limitations:

class User { public int id = 0; }

public class Main
{ public static void main( final java.lang.String[] args )
{ User user = new User();
java.lang.System.out.println
( new de.dclj.ram.notation.junobjects.Junobjects().dump( user )); }}

< &objectmap
object =
< &User
id =
< &int 0 >>>

The library is avaiable at

http://www.purl.org/stefan_ram/pub/ram-jar

>I know you could make one using reflection; my question is *has*
>someone made one, one that is tunable (ie: that can override objects'
>toString() methods, shows only so many levels of output, etc. Being
>able to override toString() for subobjects is very important because
>Java so helpfully provides a somewhat meaningless toString() function
>for Object)


Now I see, you already knew about »toString()«!

Yes, my dumper uses reflection, but does not care about
»toString()« implementations.

However, your idea to use overriding to control the dumper
is good!

The source code of my dumper is available under the GPL as
indicated on the web site.

More examples:

The next example shows a self reference:

class Ship
{ Ship ship;
final java.lang.String a0 = "abc";
final java.lang.String a1 = "abc";
final java.lang.String a2 = new java.lang.String( a1 ); }

public class Main
{ public static void main( final java.lang.String[] args )
{ Ship ship = new Ship(); ship.ship = ship; // add self reference
java.lang.System.out.println
( new de.dclj.ram.notation.junobjects.Junobjects().dump( ship )); }}

outputs:

< &objectmap
object =
< &Ship
a0 =
< &java.lang.String zz0 >
a1 =
< &java.lang.String zz0 >
a2 =
< &java.lang.String zz1 >
ship =
< &Ship object >>
zz0 =
< &java.lang.String
count =
< &int 3 >
hash =
< &int 0 >
offset =
< &int 0 >
value =
< &[char[]] a b c >>
zz1 =
< &java.lang.String
count =
< &int 3 >
hash =
< &int 0 >
offset =
< &int 0 >
value =
< &[char[]] a b c >>>

public class Main
{ public static void main( final java.lang.String[] args )
{ final java.util.HashMap<java.lang.String,java.lang.Integ er> hashMap
= new java.util.HashMap<java.lang.String,java.lang.Integ er>();
hashMap.put( "a", 1 );
hashMap.put( "b", 2 );
java.lang.System.out.println
( new de.dclj.ram.notation.junobjects.Junobjects().dump( hashMap )); }}

< &objectmap
object =
< &java.util.HashMap
entrySet =
< >
loadFactor =
< &float 0.75 >
modCount =
< &int 2 >
size =
< &int 2 >
table =
< &[java.util.HashMap.Entry[]]
< null >
< null >
< null >
< null >
< &java.util.HashMap.Entry zz0 >
< null >
< null >
< &java.util.HashMap.Entry zz1 >
< null >
< null >
< null >
< null >
< null >
< null >
< null >
< null >>
threshold =
< &int 12 >>
zz0 =
< &java.util.HashMap.Entry
hash =
< &int 100 >
key =
< &java.lang.String zz2 >
next =
< >
value =
< &java.lang.Integer zz3 >>
zz1 =
< &java.util.HashMap.Entry
hash =
< &int 103 >
key =
< &java.lang.String zz4 >
next =
< >
value =
< &java.lang.Integer zz5 >>
zz2 =
< &java.lang.String
count =
< &int 1 >
hash =
< &int 98 >
offset =
< &int 0 >
value =
< &[char[]] b >>
zz3 =
< &java.lang.Integer
value =
< &int 2 >>
zz4 =
< &java.lang.String
count =
< &int 1 >
hash =
< &int 97 >
offset =
< &int 0 >
value =
< &[char[]] a >>
zz5 =
< &java.lang.Integer
value =
< &int 1 >>>

class A { final B b; final C c;
public A( final B b, final C c ){ this.b = b; this.c = c; }}
class B { final int m = 1; final java.lang.String x = "X";
final D d;
public B( final D d ){ this.d = d; }}
class C { final int n = 2; final java.lang.String y = "Y";
final D d;
public C( final D d ){ this.d = d; }} class D {}

public class Main
{ public static void main( final java.lang.String[] args )
{ final D d = new D();
final B b = new B( d );
final C c = new C( d );
final A a = new A( b, c );
java.lang.System.out.println
( new de.dclj.ram.notation.junobjects.Junobjects().dump( a )); }}

< &objectmap
object =
< &A
b =
< &B zz0 >
c =
< &C zz1 >>
zz0 =
< &B
d =
< &D zz3 >
m =
< &int 1 >
x =
< &java.lang.String zz2 >>
zz1 =
< &C
d =
< &D zz3 >
n =
< &int 2 >
y =
< &java.lang.String zz4 >>
zz2 =
< &java.lang.String
count =
< &int 1 >
hash =
< &int 0 >
offset =
< &int 0 >
value =
< &[char[]] X >>
zz3 =
< &D >
zz4 =
< &java.lang.String
count =
< &int 1 >
hash =
< &int 0 >
offset =
< &int 0 >
value =
< &[char[]] Y >>>

 
Reply With Quote
 
ed.peschko@gmail.com
Guest
Posts: n/a
 
      04-06-2009

> I'm unfamiliar with Perl and it's libraries, so I have to ask -- what
> would you do with such a thing?


Avoid having to write toString() functions for all of your modules,
sprinkle your code with printlns without needing to worry about
implementation, get good defaults for printing out structures like
List<String>..

>
> For debugging, my debugger shows me all I need, with out any work
> whatsoever on my part. *I haven't looked into logging utilities but the
> default ones do take an object as a parameter. *This might just invoke
> toString() however. *Then there's Serialization and XMLEncoder/Decoder,
> which might be consdiered "stadardized" forms also.


yes, if you can get that ability inside of jdb, let me know. And I
don't have access to x11 on the place where I need to run the code, so
if you know how to run code on a server, serialize input and output
via an ssh shell, and get the output into an intellij/eclipse IDE, I'd
love to know that too. Use an IDE is great as long as you have access
to an IDE.

Ed
 
Reply With Quote
 
ed.peschko@gmail.com
Guest
Posts: n/a
 
      04-06-2009
On Apr 6, 1:20*pm, (E-Mail Removed)-berlin.de (Stefan Ram) wrote:
> (E-Mail Removed) writes:
> >Is there an equivalent module in java?

>
> * Partially, there is no need for :ata:umper:umper, because
> * Java already has the, er, message »toString()« for this purpose.
>
> * But, »toString()« has difficulties. To get a meaningful dump,
> * one does not use »array.toString()«, but
> * »java.util.Arrays.toString( a )«.
>


Thanks.. I'll give it a try.

Ed
 
Reply With Quote
 
horos11@gmail.com
Guest
Posts: n/a
 
      04-06-2009
On Apr 6, 1:46*pm, Patricia Shanahan <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> >> I'm unfamiliar with Perl and it's libraries, so I have to ask -- what
> >> would you do with such a thing?

>
> > Avoid having to write toString() functions for all of your modules,
> > sprinkle your code with printlns without needing to worry about
> > implementation, get good defaults for printing out structures like
> > List<String>..

>
> ...
>
> This idea would be harder to implement usefully in Java than in many
> other languages. The problem is pointer chasing without getting into
> perpetual loops.
>
> Java has no concept of a structure containing another structure, only
> having a pointer to it. If you do not follow pointers, all you get is
> the values of primitive fields. If you do follow pointers, there is no
> way to distinguish the ones that could lead to loops.
>
> This is not a problem for interactive display, because the user chooses
> when to expand a pointer target.
>
> Patricia


Of course, it would be relatively difficult, but perl has the ability
to self-reference as well - and Data:umper works with it just fine.
You need to keep a list of 'seen' pointers via address and recognize
when they have been printed out or not.

In any case, it looks like xstream is what I want (or close to it) -
it has the additional benefit of being able to serialize objects in a
readable xml format as well for transfer over a network. Stefan's
Dumper class (above) looks interesting too..

Ed
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      04-06-2009
(E-Mail Removed) wrote:

> yes, if you can get that ability inside of jdb, let me know. And I
> don't have access to x11 on the place where I need to run the code, so
> if you know how to run code on a server, serialize input and output


Ah, so. Debugging then. Yes, this is the worst possible situation to
be in: debugging a problem without an adequate debugging harness.

Consider copying the code locally and reproducing the problem locally so
that you can attach a debugger.

Or bite the bullet and install whatever you need to debug on the server,
and attach to that. It's just a network connection.

Just curious: is your boss paying you by the hour for this? Or did you
get roped into it on a contract?

 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      04-06-2009
Patricia Shanahan <(E-Mail Removed)> wrote:
>This idea would be harder to implement usefully in Java than in many
>other languages. The problem is pointer chasing without getting into
>perpetual loops.


That's no harder in Java than anywhere else. You need to handle multiple
references to the same object, which you can check with == rather than
..equals. IdentityHashSet is an easy way to do so. In fact, Perl does support
arbitrary reference graphs, and Data:umper handles it.

It's not as compact a format as Perl's Data:umper, and it takes more work
than just an eval to reconstitute, but java.beans.XMLEncoder is pretty close
to what you're looking for. A version that is perl-compatible (especially
read and write compatible) would actually be a pretty cool thing.
--
Mark Rafn (E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      04-06-2009
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram) writes:
> ( new de.dclj.ram.notation.junobjects.Junobjects().dump( ship )); }}


I have been informed that the source code from my Zip file
cannot be compiled to create the JAR. Sorry. Here is a Fix:

To create the JAR from the ZIP:

- download and extract the ZIP file, then extract the inner ZIP file.

- now rename 11 offending Java files, so that the compiler will not
try to compile them:

In de\dclj\ram\notation\bauer\prescanner rename:
Cursor.java -> Cursor.txt
Pretoken.java -> Pretoken.txt
PretokenListBuilder.java -> PretokenListBuilder.txt
Autotoken.java -> Autotoken.txt

In de\dclj\ram\notation\bauer\scanner rename:
Token.java -> Token.txt

In de\dclj\ram\notation\bauer\parser rename:
OperatorToken.java -> OperatorToken.txt
OperatorStack.java -> OperatorStack.txt

In de\dclj\ram\system\iteration rename:
TupleNesting.java -> TupleNesting.txt
IntegralRange.java -> IntegralRange.txt

In de\dclj\ram\javax\swing rename:
GenericMenuItem.java -> GenericMenuItem.txt

In de\dclj\ram\application\club rename:
Main.java -> Main.txt

- Then the library can be built and used with
a »Main.java« from the above post:

cd src
find . -name *.java -print >sources.txt
javac -d ../lib -source 1.6 -target 1.6 -encoding UTF-8 @sources.txt
cd ..
cd lib
find . -name *.class -print >classes.txt
jar cf ram.jar @classes.txt
javac -classpath .;ram.jar Main.java
java -cp .;ram.jar Main

 
Reply With Quote
 
horos11@gmail.com
Guest
Posts: n/a
 
      04-07-2009
well, re-implementing the world isn't really feasible, since there are
network rules about which machines attach to which, and that rules out
connections as well (everything is 3rd party hosted)..

But in any case, there are benefits of having a dumper object
regardless of the API you use, IMO. If the Serialization order is
reproducable (ie: the same objects always go to the same
representations) then you can do a visual diff against two objects and
get a concise idea of changes.

Or, you can store (in dev and test anyways) representations of objects
as they happen by external testing - ie: you can see at a glance what
data problems might cause a given bug to happen.

And of course, if the Serialization is both human and java writable
and human and java readable, you can use it to develop unit tests for
your application, sometimes many unit tests from one source.

So.. many uses, I would say.

Ed


On Apr 6, 1:59*pm, Mark Space <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > yes, if you can get that ability inside of jdb, let me know. And I
> > don't have access to x11 on the place where I need to run the code, so
> > if you know how to run code on a server, serialize input and output

>
> Ah, so. *Debugging then. *Yes, this is the worst possible situation to
> be in: debugging a problem without an adequate debugging harness.
>
> Consider copying the code locally and reproducing the problem locally so
> that you can attach a debugger.
>
> Or bite the bullet and install whatever you need to debug on the server,
> and attach to that. *It's just a network connection.
>
> Just curious: is your boss paying you by the hour for this? *Or did you
> get roped into it on a contract?


 
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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hot Requirements: 1.Sr Java Developer,2.Java Developer (Java with EJB) Isaac Java 0 01-20-2011 08:41 PM
hey i am just started java,, can anyone tell me the use ,application, why java , importance of java.. manish sahu Java 3 02-14-2008 12:00 AM
[JAVA] [EVALUATION] - The Java Failure (Sorry: The Java(tm) Failure) Ilias Lazaridis Java 0 02-01-2005 10:32 AM
JAVA VIRTUAL MUCHINE OR SUN JAVA Fernando Kohan Firefox 1 11-14-2004 02:04 AM
Job to convert Java App 1.3.1 to Java Newest of Java Michael Kintner Java 0 11-30-2003 04:42 AM



Advertisments