Thursday, February 17, 2011

ruby and json vs xml marshalling

I've been watching a silent war rage between the ruby community with its preference for JSON and RESTful services and the Java community with its preference for XML and SOAP.

The rdoc for Nokogiri snarks at the Java community with a simple quote:

"XML is like violence - if it doesn’t solve your problems, you are not using enough of it."

Like many devs caught in the middle of this war (namely having to integrate Rails apps with Java SOA backends) I'm never quite satisfied with the state of Ruby XML marshaling. Sure there are a dozen gems to address this -- they always vary between giant thousand-object DOMs that leak memory like a sieve, arcane forced DSL syntaxes, or simple to use, but incomplete APIs. So I started to think about hand rolling "yet another XML marshaller" for Ruby.

My first idea was to simplify the XML DOM, so that everything is unmarshaled as an element. Then I can use a simple hierarchal accessor, the "dot" operator on the Ruby object side:

On the face of it, I thought that looked pretty sweet. But then I considered remarshaling this object as XML. Doh! There's no way to tell whether "mike" is an element or an attribute... well I could stick a little metadata on there to remind me later, but it's starting to look like I stumbled into the same path that so many other Ruby gems had trying to solve this problem. And it's not for lack of trying -- Ruby devs have tried many ways to resolve this ambiguity with various results, but not a lot of clear wins -- there's always a "gotcha" in there that makes you wince when you have to handle it. (it's the same wince that Java devs have when they switch axis jars on one server but not another.)

I sat back in disgust and thought to myself,

"wow, this would be so much easier if it was just JSON..."

then it hit me... JSON is easy because round-trip marshaling doesn't introduce any ambiguity about elements vs attributes! Everything is an element!!!

No wonder JSON is so easy!

No comments: