Jackson: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 68: Line 68:
==Specifying a Date Format==
==Specifying a Date Format==


{{External|https://www.baeldung.com/jackson-serialize-dates}}
[[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]}}


=Annotations=
=Annotations=

Revision as of 00:26, 7 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.

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

Thread Safety

ObjectMapper 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, ObjectWriter and ObjectReader can be constructed by ObjectMapper. These objects are fully immutable, thread-safe and cheap to create?

Configuration

ObjectMapper can be configured as follows:

  • 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
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Specifying a Date Format

ISO 8601 date and time format can be automatically handled (converted from and to) by the ObjectMapper, as follows:

import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.StdDateFormat;

objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.setDateFormat(new StdDateFormat());

More:

Jackson Serialize Dates on Baeldung

Annotations

Maven Support

<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>