Jackson: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(21 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==
==<span id='Data_Binding'></span>Simple 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.  
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.


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.
{{Internal|Jackson Simple Data Binding|Simple Data Binding}}


===Simple Data Binding===
==Full Data Binding==


{{Internal|Jackson Simple Data Binding|Jackson Simple 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===
{{Internal|Jackson Full Data Binding|Full Data Binding}}


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


=ObjectMapper=
==<span id='Thread_Safety'></span><span id=''></span><span id='Configuration'></span><span id='Specifying_a_Date_Format'></span>ObjectMapper==


{{Internal|Jackson ObjectMapper|ObjectMapper}}
{{Internal|Jackson ObjectMapper|ObjectMapper}}


==Thread Safety==
==JsonGenerator==
 
<tt>ObjectMapper</tt> is thread safe, so it does not have to be created for each request that needs serialization/deserialization.
 
It is recommended to declare a static ObjectMapper instance that should be shared as much as possible.
 
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,
 
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>
 
==Configuration==
 
===Mapper Features===
 
====Annotation Use====
 
<tt>ObjectMapper</tt> can be configured to use or not annotation introspection. By default is true.
 
<syntaxhighlight lang='java'>
om.configure(MapperFeature.USE_ANNOTATIONS, true);
</syntaxhighlight>
 
====Use Getters as Setters====
 
This feature determines whether the "getter" methods that handle Collections and Maps can be used for purpose of getting a reference to a Collection and Map to modify the property, without requiring a setter method. This is similar to how JAXB framework sets Collections and Maps. Note that such getters-as-setters methods have lower precedence than setters, so they are only used if no setter is found for the Map/Collection property. The feature is enabled by default.
 
<syntaxhighlight lang='java'>
om.configure(MapperFeature.USE_GETTERS_AS_SETTERS, true);
</syntaxhighlight>
 
====Propagate Transient Marker====
 
This feature determines how <tt>transient</tt> modifier for fields is handled: if disabled, it is only taken to mean exclusion of the field as accessor; if true, it is taken to imply removal of the whole property. Feature is disabled by default, meaning that existence of <tt>transient</tt> for a field does not cause Jackson to ignore getters or setters just ignoring the use of field for access.
 
<syntaxhighlight lang='java'>
om.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, false);
</syntaxhighlight>
 
===Deserialization Features===
 
====Fail on Ignored Properties====
 
This feature determines what happens when a property that has been explicitly marked as ignorable is encountered in input: if feature is enabled, JsonMappingException is thrown; if false, property is quietly skipped. The feature is disabled by default so that no exception is thrown. Another way to look at it is as configuration 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'>
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
</syntaxhighlight>
 
==Specifying a Date Format==
 
[[ISO 8601]] date and time format can be automatically handled (converted from and to) by the ObjectMapper, as follows:
 
<syntaxhighlight lang='java'>
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.StdDateFormat;
 
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.setDateFormat(new StdDateFormat());
</syntaxhighlight>
 
More: {{External|[https://www.baeldung.com/jackson-serialize-dates Jackson Serialize Dates on Baeldung]}}


<font color=darkgray>TODO</font>
==View==
<font color=darkgray>TODO</font>
=Annotations=
=Annotations=


* [[@JsonIgnoreProperties]]
* [[@JsonIgnoreProperties]]
* [[@JsonFormat]]
* [[@JsonSerialize]]
* [[@JsonInclude]]


=Maven Support=
=Subjects=
 
* [[YAML conversion to JSON with Jackson]]
<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:
 
<pre>
<dependency>
    <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