If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Tuesday, March 27, 2012

Jackson now also supports XML

Today i had the need again to serialize some plain old Java objects to XML. Having worked before with XStream and Castor I looked into Jackson today.
I quickly discovered that the ToXmlGenerator could be setup by calling the createJsonGenerator() on the XMLFactory. That sounds strange at first and it was no surprise to me that due to this fact you have to write a lot of boilerplate code to get the job done. For serializing JSON you have to know if you are dealing with booleans, strings, numbers and so on. It's all about getting the Quotes "" right. For XML this is not the case however and I immediately started writing a JacksonXMLGeneratorHelper to ease the pain. Not only did this reduce the LOC count drastically but i think the code became also much more readable.

The output from the two unit tests produces the same result as expected.

No comments:

Post a Comment