Jackson: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(29 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=External=


* Latest Release: https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.9
* Jackson Documentation Hub: https://github.com/FasterXML/jackson-docs
* Jackson Documentation Hub: https://github.com/FasterXML/jackson-docs
* Jackson JSON Processor Wiki http://wiki.fasterxml.com/JacksonHome
* Jackson JSON Processor Wiki http://wiki.fasterxml.com/JacksonHome
Line 9: Line 10:
** Jackson Core Annotations https://github.com/FasterXML/jackson-annotations/wiki
** Jackson Core Annotations https://github.com/FasterXML/jackson-annotations/wiki
** Jackson Databind https://github.com/FasterXML/jackson-databind/wiki
** Jackson Databind https://github.com/FasterXML/jackson-databind/wiki
* http://www.javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind/2.9.7


=Internal=
=Internal=
Line 17: Line 19:


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=
Line 24: Line 45:
==Streaming API==
==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|Tree Model]] and [[#Data_Binding|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: {{Internal|Jackson Streaming API|Jackson Streaming API}}
The Streaming API reads and writes JSON as a series of discrete events, in a mode called "incremental parsing/generation". The other methods ([[#Tree_Model|Tree Model]] and [[#Simple_Data_Binding|Simple Data Binding]] and [[#Full_Data_Binding|Full 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: {{Internal|Jackson Streaming API|Streaming API}}


==Tree Model==
==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: {{Internal|Jackson Tree Model|Jackson 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: {{Internal|Jackson Tree Model|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|tree model]], where JSON is translated to a <tt>JsonNode</tt> 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===
==<span id='Data_Binding'></span>Simple Data Binding==


{{Internal|Jackson Simple Data Binding|Jackson Simple Data Binding}}
Jackson extracts data from JSON and initializes an in-memory Java <tt>Map</tt>/<tt>List</tt>/<tt>String</tt>/<tt>Number</tt>/<tt>Boolean</tt>/<tt>null</tt> hierarchy. Jackson specialized data representation types such as [[Jackson_Tree_Model#JsonNode|JsonNode]] are not used.


===Full Data Binding===
{{Internal|Jackson Simple Data Binding|Simple Data Binding}}


{{Internal|Jackson Full Data Binding|Jackson Full Data Binding}}
==Full Data Binding==


=ObjectMapper=
Jackson converts JSON to any Java bean type, and any Java bean type can be automatically written as JSON. Data binding is arguably the most convenient of all three methods.


==Thread Safety==
{{Internal|Jackson Full Data Binding|Full Data Binding}}


<tt>ObjectMapper</tt> is thread safe, so it does not have to be created for each request that needs serialization/deserialization.
=Concepts=


It is recommended to declare a static ObjectMapper instance that should be shared as much as possible.
==<span id='Thread_Safety'></span><span id=''></span><span id='Configuration'></span><span id='Specifying_a_Date_Format'></span>ObjectMapper==


The only problem with this approach is that the configuration of the mapper cannot be changed after it was shared, otherwise clients will rely on invalid assumptions,
{{Internal|Jackson ObjectMapper|ObjectMapper}}


With 2.0 and above, <tt>ObjectWriter</tt> and <tt>ObjectReader</tt> can be constructed by <tt>ObjectMapper</tt>. These objects are fully immutable, thread-safe and <font color=darkgray>cheap to create?</font>
==JsonGenerator==


==Configuration==
<font color=darkgray>TODO</font>
 
==View==
<tt>ObjectMapper</tt> can be configured as follows:
<font color=darkgray>TODO</font>
 
=Annotations=
* To ignore fields that appear in the JSON serialized format, but are not declared as members in the type. The same effect can be achieved by annotating the type with [[@JsonIgnoreProperties]]
 
<syntaxhighlight lang='java'>
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
</syntaxhighlight>
 
=Maven Support=
 
<pre>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.7</version>
</dependency>
</pre>


If the [[Jackson Tree Model|tree model]] is used, this is also required:
* [[@JsonIgnoreProperties]]
* [[@JsonFormat]]
* [[@JsonSerialize]]
* [[@JsonInclude]]


<pre>
=Subjects=
<dependency>
* [[YAML conversion to JSON with Jackson]]
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.7</version>
</dependency>
</pre>

Latest revision as of 01:52, 8 February 2020

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 methods (Tree Model and Simple Data Binding and Full 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:

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:

Tree Model

Simple Data Binding

Jackson extracts data from JSON and initializes an in-memory Java Map/List/String/Number/Boolean/null hierarchy. Jackson specialized data representation types such as JsonNode are not used.

Simple Data Binding

Full Data Binding

Jackson converts JSON to any Java bean type, and any Java bean type can be automatically written as JSON. Data binding is arguably the most convenient of all three methods.

Full Data Binding

Concepts

ObjectMapper

ObjectMapper

JsonGenerator

TODO

View

TODO

Annotations

Subjects