日期:2014-05-16  浏览次数:20312 次

7 Killer Features that set Jackson apart from competition (Java JSON)

7 Killer Features that set Jackson apart from competition (Java JSON)

Jackson JSON processor is a well-established Java JSON processor, known to offer things like:

  • Simple and convenient parsing of JSON, with conversion to/from Java objects
  • Extensive configurability with annotations, settings
  • Ultra-fast performance with streaming parsing as well as full data binding

But many other Java JSON libraries offer convenience and configurability; and performance is not always amongst most important aspects to users.
So why should a Java developer choose Jackson over competition?

Above short list of features is actually but a tip of the iceberg of Jackson functionality. True, these three general areas are important; but in a way they are just a starting point, the baseline that all JSON processors should offer to be worthy of even being considered as the tool to use. But beyond this baseline there is much much more that could and should be offered; and this is where Jackson really delivers.

So let's look at sampling of 7 -- nice round number -- "killer features" that set Jackson years ahead of the competition, presented in order of being introduced (starting with version 1.0, last ones being added by 1.6).

1. Multiple processing modes, all co-operating nicely

Starting with the basics, there are multiple ways to consume and produce JSON data. Although many libraries offer just a single way (processing model), there are essentially three complementary ways (read "There are Tree ways... " for longer explanation) to process JSON:

  • Incremental ("streaming") parsing and generation: high-performance, low-overhead sequential access. This is the lowest-level processing method, comparable to SAX and Stax APIs for XML processing. All packages must have such a parser internally, but not all expose it.
  • Tree-based data model ("DOM for JSON"). Tree is a natural conceptual model to present JSON content; and as such many packages offer functionality to operate on JSON as a logical tree. This is a flexible model, well-suited for some tasks, and great for prototyping or ad hoc access.
  • Data-binding (JSON to/from POJOs). Ultimate in convenience, and typically more efficient than tree-based access, data binding is usually the most natural fit for Java developers. It is used with most Java REST frameworks, such as JAX-RS

Despite obvious benefits of offering multiple views, each with their own optimal use cases, few (if any?) other Java JSON package offers these canonical processing models.
Most offer just one (org.json exposes data as Trees; Gson implements data binding, for example). Jackson offers the full set; all modes fully supported, and best of all, in such a way that it is easy to convert between modes, mix and match. For example, to process very large JSON streams, one typically starts with a streaming parser, but uses data binder to bind sub-sections of data into Java objects: this allows processing of huge files without excessive memory usage, but with full convenience of data binding.

2. Use any constructors, factory methods that you want (not just default zero-arg one!)

Most data binding tools (for JSON as well as XML) require one to define and use zero-argument constructor, to instan