Jackson: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(59 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
* Jackson in Five Minutes http://wiki.fasterxml.com/JacksonInFiveMinutes
* Jackson in Five Minutes http://wiki.fasterxml.com/JacksonInFiveMinutes
* Jackson JSON Tutorial http://www.baeldung.com/jackson
* Jackson JSON Tutorial http://www.baeldung.com/jackson
* javadocs:
** Jackson Core https://github.com/FasterXML/jackson-core/wiki
** Jackson Core Annotations https://github.com/FasterXML/jackson-annotations/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 13: 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 20: 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 concepts behind the Streaming API are similar to those of [[JAXP_StAX#jackson|StAX]]. The Streaming API has the lowest overhead and its the fastest of all methods. 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. All content to read or write has to be processed in the order the input comes or the output has to go out. Random access is not possible. Also, no Java objects are created, unless specifically requested, and even then only very basic types are supported: String and byte[].
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}}


The central elements of the Streaming API are the ''parsers'' (<tt>org.codehaus.jackson.JsonParser</tt>) that are used for reading JSON content, and the ''generators'' (<tt>org.codehaus.jackson.JsonGenerator</tt>) that are used for writing Java state out as JSON.
==Tree Model==


This is an example of how to create a parser that reads JSON from a file and parses it:
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}}


This is an example of how to create a generator that writes JSON from Java state:
==<span id='Data_Binding'></span>Simple Data Binding==
 
==Tree Model==


Tree model is arguably the most flexible of all three methods.
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.


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


==Full Data Binding==


Data Binding is arguably the most convenient of all three methods.
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.  


=JSON to Java=
{{Internal|Jackson Full Data Binding|Full Data Binding}}


{{Internal|Jackson JSON to Java|JSON to Java}}
=Concepts=


=Java to JSON=
==<span id='Thread_Safety'></span><span id=''></span><span id='Configuration'></span><span id='Specifying_a_Date_Format'></span>ObjectMapper==


{{Internal|Jackson Java to JSON|Java to JSON}}
{{Internal|Jackson ObjectMapper|ObjectMapper}}


==JsonGenerator==


<font color=red>TODO and delete: https://home.feodorov.com:9443/wiki/Wiki.jsp?page=Jackson</font>
<font color=darkgray>TODO</font>
==View==
<font color=darkgray>TODO</font>
=Annotations=


=Maven Support=
* [[@JsonIgnoreProperties]]
* [[@JsonFormat]]
* [[@JsonSerialize]]
* [[@JsonInclude]]


<pre>
=Subjects=
<dependency>
* [[YAML conversion to JSON with Jackson]]
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</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