Monday, February 02, 2009

Json processing with Jackson: Which method to use?

Now that we have reviewed all the methods (specifically, Stream-of-Events, Data Binding and Tree Model), the remaining question is: which one is right for me?
There are no hard and fast rules, but here are couple of perspectives that may be useful in sorting it all out.

1. If all else fails, bind to objects

My personal favorite is the data binding, and barring specific reasons to contrary, data binding seems like a good default choice. If it doesn't work, other alternatives will still be available; but usually it "Just Works". And I like that.

2. For extreme efficiency, use Stream-of-Events

For developers with extremely frugal resource constraints (memory, cpu), nothing beats the "raw" streaming API. It uses minimum amount of memory, does not construct any more objects than are absolutely needed, and does this very fast. There's no scroogier way to deal with Json. It is not the most convenient way, but it is efficient and flexible. You just have to be a DIY handy man; and the interface is WYWIWYG (What You Write Is What You Get). No syntactic sugar, just a bit more monkey code.

3. For write-only use cases, Stream-of-Events does just fine

Given how simple it is generate (write) Json content, there are often no particular reasons to use anything more complicated than a JsonGenerator. That is, when input does not come from a Json source (if it did, other alternatives might be more tempting).

In fact, considering that if you don't start with any Json-based construct, streaming API may be even simpler than Tree Model based one or data binding: after all, to use the Tree Model you must build a tree; and for data binding have to construct objects to convert to Json. That is just overhead if the only purpose is to generate Json.

4. For flexible traversal, try the Tree Model

If you have to do lots of traversal, especailly using dynamic rules, and possibly incomplete and/or non-regular content, Tree Model is often often the best choice.

5. Tree Model works well for prototyping

As a special case for dynamic traversal and access, prototyping (including cases where Json interchange data format changes rapidly) is one case where Tree Model can be useful. The problem with Data Binding is the very tight coupling between Java classes and resulting Json. It is often less work to change the code that traverses Tree Model than to change the object model AND code that uses objects. And there is generally less code to traverse a tree than do deal with event streams.

6. Other suggestions?

Now that I have dumped my initial thoughts, I realize how scattered they are. :-)

I'm sure I am missing obvious things, or may be grouping unrelated things together. Please let me know what I got wrong!

blog comments powered by Disqus

Sponsored By

Related Blogs

(by Author (topics))

Powered By

About me

  • I am known as Cowtowncoder
  • Contact me
Check my profile to learn more.