Joe Walnes
  Blog



Recent Entries

Creative uses of Hamcrest matchers

Hamcrest 1.1 released

Testing on the Toilet

Building testable AJAX apps (Does my button look big in this?)

QDox is back - 1.6 released

Java and .NET RESTful interoperability with XStream

I've joined Google

OSCon: SiteMesh, SiteMesh, SiteMesh, SiteMesh

Flexible JUnit assertions with assertThat()

SiteMesh and Content Management @ O'Reilly OpenSource Conference

XStream 1.1.2 released. Java 5 Enums, JavaBeans, field aliasing, StAX, and more...

VB.Net is the bestest

XStream 1.1.1 released

Accessing generic type information at runtime

XStream 1.1 released

JUnit tip: Setting the default timezone with a TestDecorator

XStream: how to serialize objects to non XML formats

How my backflip went...

Backflippin' in 4 hours.

Is 100% test coverage a BAD thing?

Looking back at the SiteMesh HTML parser

The road ahead for SiteMesh 3

Joe's Backflipping for Autistic Research - time is nearly up...

SiteMesh 2.2 Released

Advanced SiteMesh

More... [RSS | RDF]

About Joe Walnes

I am a software engineer for Google, based in London.

Open Source

WebStuff (coming soon)

XStream

ActiveMQ

SiteMesh

QDox

nMock

jMock

Pico Container

Nano Container

OpenSymphony

Squiggle

MockDoclet

MockObjects

Jelly

Groovy

PatternStitcher

XJB

Books

Java Open Source Programming, Wiley JSP Site Design, Wrox

Talks

Mock Roles, not Objects
October 26 2004, Vancouver, Canada. OOPSLA'04

Personal Development Practices Map
June 24 2004, Salt Lake City, Utah. Agile Development Conference

SiteMesh.NET and ASP.NET MasterPages
May 20 2004, Bangalore, India. Bangalore .NET User Group

Mock Objects: Driving Top Down Development
March 29 2004, St Neots, UK. OT2004

Mock Objects
December 2 2003, London, UK. XP Day 3


XStream: how to serialize objects to non XML formats

As you know, XStream makes it easy to serialize objects to XML:

Person person = ...;
xstream.toXML(out);

Producing:

<com.blah.Person>
  <firstName>Joe</firstName>
  <lastName>Walnes</lastName>
  <homePhone>
    <areaCode>123</areaCode>
    <number>433535</number>
  </homePhone>
  <cellPhone>
    <areaCode>4545</areaCode>
    <number>4534</number>
  </cellPhone>
</com.blah.Person>

I often use this approach whilst debugging to dump out the contents of an object. It works, but my eyes just aren't that good at parsing XML.

By creating an alternative writer implementation, XStream can be used to serialize objects in other formats:

Person person = ...;
xstream.marshal(stuff, new AnAlternativeWriter(out));

Producing the slightly more digestible:

com.blah.Person
  firstName = Joe
  lastName = Walnes
  homePhone
    areaCode = 123
    number = 433535
  cellPhone
    areaCode = 4545
    number = 4534

To gain roundtrip serialization/deserialization support in alternative formats to XML, you need to provide your own implementations of both HierarchicalStreamWriter and HierarchicalStreamReader.

Comments

Rickard

As noted here you can also use XStream to serialize/deserialize objects directly into an XML:DB database such as Xindice or eXist, by using the SAX integration. That way you have a nice "lightweight" object storage that you still can use XML tools to analyze and work with.

Rickard

Damnit, the link was stripped from my comment. Oh well, here it is:
http://jroller.com/page/rickard/20041218

Mike Williams

Nice one, Joe.

Had a crack at YAML (http://yaml.org) yet?

Aslak Hellesoy

YAML would be awesome - it would allow for very easy Java/Ruby interop!

Aslak Hellesoy

With a YamlStreamWriter and a YamlStreamReader XStream would become a fully working Java YAML implementation. They shouldn't be that hard to implement methinks.

Joe Walnes

I imagine YAML would be quite easy to do.
As would a mechanism for serializing a complete object graph to java.util.Properties format.

Something I'm more interested in doing is a binary representation that's a lot more compact than the standard Java serialization format.

Rickard

Joe, my integration with the Xindice XML database is a little interesting with regard to the binary representation. If I understand it correctly it stores the DOM internally, but uses a symbol table to make it small. I suppose you could do something similar as a custom serializer, i.e. write a symbol table along with a "binary" representation of the output tree. That should work ok. It'd still be XML, just a little more compact than the text variant.

However, if you search for "binary XML" on Google there seems to be a lot of heated arguments about whether binary XML is a good idea at all.

*shrug*

How about serializing to the serialised object formats of other languages, like Python, Ruby or PERL?

Aslak Hellesoy

Writing YAML would be pretty easy (YamlStreamWriter). Implementing a YamlStreamReader that reads YAML would probably be a bit harder, since the YAML spec is not trivial (http://www.yaml.org/spec/).

A YamlStreamReader could be written based on the following approaches:

1) Port Syck (http://whytheluckystiff.net/syck/) from C to Java (manually or with the help of a C-to-Java translator)

2) Implement a YAML parser from scratch, using a similar approach to http://homepages.ihug.com.au/~zenaan/zenaan/files/ more notably this (retired?) one based on ANTLR (http://homepages.ihug.com.au/~zenaan/zenaan/files/yamljava/)
Perhaps a JFlex grammar could be generated from the YAML spec page?

3) Implement a lo-fi parser that can only parse "simple" YAML documents.

I would think option 1 or 2 are the best options in the long run.

Answer to Anonymous Coward regarding object formats like Python, Ruby or PERL: YAML is the way to go I think. All of these languages support YAML.

Francois Henderson

Hi Joe,
I posted a question on the XStream user mailing list, without answer yet. May be you have an idea of how I can handle deserializization of XML file that contains a repeted object. I used the "Frequently Ask Questions" example to do my testing. Create XML from Java Class Object is pretty straith forward but repopulate your objects when there is more than one of the same object is not really trivial. If you have any input that will be really great. Thanks in advance. Cheers.

katalog

Indeed, compact binary format will be an awesome addition to the excellent XStream package. Default Java serialization has its problems and there is the need for fast, memory efficient and compact binary serialization. BTW, I am sure you have looked at Hessian and Burlap (from the makers of the Resin app. server)

Name:
Email:
URL:

ThoughtBloggers

Martin Fowler

Dan North

Aslak Hellesoy

Darren Hobbs

Geoff Oliphant

Mike Roberts

Chris Stevenson

Jon Tirsen

Loads More...

Agile Bloggers

Ken Arnold

Ward Cunningham

Brian Marick

Robert Martin

Bret Pettichord

Java Bloggers

Ara Abrahamian

Mike Cannon-Brookes

Vincent Massol

Bob McWhirter

Rickard Oberg

Joseph Ottinger

James Strachan

Hani Suleiman

Communities

eXtreme Tuesday Club (XTC)

Thursday GeekSpeek

ThoughtWorks GeekNight

London Java Meetup

The Codehaus

[RSS | RDF]
© 2001-2004, Joe Walnes

Powered by SiteMesh and Moveable Type.