Jackson: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 17: Line 17:


Jackson is a Java library for processing JSON data format. It has support for marshaling and unmarshalling Java to and from JSON. It has a [[JAX-RS Concepts#Content_Handler|JAX-RS content handler]] that can automatically convert between Java objects and JSON, and vice-versa. It can generate JSON schemas from a Java object model.
Jackson is a Java library for processing JSON data format. It has support for marshaling and unmarshalling Java to and from JSON. It has a [[JAX-RS Concepts#Content_Handler|JAX-RS content handler]] that can automatically convert between Java objects and JSON, and vice-versa. It can generate JSON schemas from a Java object model.
=Dependencies=
<syntaxhighlight lang='xml'>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.7</version>
</dependency>
</syntaxhighlight>
If the [[Jackson Tree Model|tree model]] is used, this is also required:
<syntaxhighlight lang='xml'>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.7</version>
</dependency>
</syntaxhighlight>


=JSON Processing Methods=
=JSON Processing Methods=

Revision as of 02:11, 14 November 2018

External

Internal

Overview

Jackson is a Java library for processing JSON data format. It has support for marshaling and unmarshalling Java to and from JSON. It has a JAX-RS content handler that can automatically convert between Java objects and JSON, and vice-versa. It can generate JSON schemas from a Java object model.

Dependencies

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.7</version>
</dependency>

If the tree model is used, this is also required:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.7</version>
</dependency>

JSON Processing Methods

Jackson offers three alternative methods for processing JSON:

Streaming API

The Streaming API reads and writes JSON as a series of discrete events, in a mode called "incremental parsing/generation". The other two methods (Tree Model and Data Binding) are built in top of it. However, this is not the most convenient method, because is relatively low level. For more details, see:

Jackson Streaming API

Tree Model

A JSON document is converted into a mutable in-memory tree representation of the JSON document. Tree model is arguably the most flexible of all three methods. For more details, see:

Jackson Tree Model

Data Binding

Jackson extracts data from JSON and initializes a in-memory Java object hierarchy. It uses non-Jackson types in the process, unlike the tree model, where JSON is translated to a JsonNode hierarchy.

In case of simple data binding, those Java types are Maps, Lists, Strings, Numbers, Booleans and nulls. In case of full data binding, Jackson converts JSON to any Java bean type. Data binding is arguably the most convenient of all three methods.

Simple Data Binding

Jackson Simple Data Binding

Full Data Binding

Jackson Full Data Binding

ObjectMapper

ObjectMapper

Annotations