Monday, July 28, 2008

Data Conversion Libraries 'R' Us

I've noticed a good chunk of traffic towards this blog is from people looking for help making their relational data (or Java classes) into XML/JSON/(insert Web-friendly format here), and vise versa. Well, this entry's for you.

Are you developing a Web application that requires object serialization, and don't want to write custom mapping files for every object? Have a database you want to access in an object-oriented way? Well, you're in luck! There are plenty of solutions that will do most of the heavy lifting of these processes for you. I will quickly introduce a few of them - Hibernate, Middlegen, XStream, and JAXB - with which you can rapidly access your database as objects, serialize the data to XML documents or JSON representations, and send it around the Internets at will.


If you want to use XML or JSON as your translated medium, XStream provides automatic Object -> XML (and back again) mechanisms, requiring only a JVM and some knowledge of Java. In my experience with XStream, it is a very good answer to a problem that otherwise took a lot of manual effort to do correctly. Best of all, you typically don't have to create any custom mappings, so if you're in a pinch, this will provide a big bang with little effort. Also worth mentioning is that XStream has a very impressive performance profile, so if your server has high throughput, XStream should not be a bottleneck.

Hibernate and Middlegen

If you have your data in a database and want to move it to Object-land, I'd advocate a classical solution: Hibernate and Middlegen. Most of you might know about Hibernate, but if you don't it's an "object/relational persistence and query service", or a service that will transition your data between its persisted state to handy Objects, based on queries you specify and mapping files you provide. The rub is in the generation of the mapping files, as it is not a task I imagine anyone enjoys. That's where Middlegen comes in; it will auto-generate mapping files based on the structure of your database, and for the most part does a fantastic job of it. It isn't perfect, but it will at least yield a result that you can tweak to your needs.

On the useability front, Middlegen can be a bit of a pain to set up and plug in, but if you have a lot of data to deal with (it generated over 100 mapping files for my case), it's worth it. For what it is, Hibernate is pretty easy to set up and run (although it has a LOT of dependencies to clutter up your classpath). The biggest issue I've come across is its performance. At first use, Hibernate will probably make you cringe. The memory footprint can get ludicrous very quickly, and typically in the amount of time it takes to bootstrap I can build an Ark. But once it's up and running on a machine with sufficient memory, it will perform quite well, querying the database and yielding a list of objects to play with in about the time it would take to do a raw SQL query. And if there are still sufficient performance issues, the cache-control mechanisms Hibernate allows for can give a big boost to your application (either minimizing memory usage or increasing throughput), depending on your traffic profile.


If your client application runs inside a Java container, or if your server-side data consists heavily of XML documents, JAXB provides a way to take these documents and unmarshall their data into Java objects. I'm not going to lie and say I've used it, read anything more about it than in a few articles, or even say it looks painless to use, but I've had it recommended to me from a few reliable sources, and figured I'd give it a mention.

Have some better or alternative methods? Used anything I've mentioned above? Please share it in the comments section!

1 comment:

johns said...

Marvelous post! Thanks for sharing post about Web Development. it's really helpful for all.