Jackson Simple Data Binding: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(27 intermediate revisions by the same user not shown)
Line 5: Line 5:
=Overview=
=Overview=


Simple data binding extracts data from JSON and initializes a in-memory Java object hierarchy. Unlike the [[Jackson#Tree_Model|tree model]], which uses <tt>JsonNode</tt>s, simple data binding uses Maps, Lists, Strings, Numbers, Booleans and nulls to represent the JSON structure.
Simple data binding extracts data from JSON and initializes an in-memory Java object hierarchy. Unlike the [[Jackson#Tree_Model|tree model]], which uses <tt>JsonNode</tt>s, simple data binding assembles the hierarchy representing the JSON data out of '''Maps, Lists, Strings, Numbers, Booleans and nulls'''. A Map/List/String/Number/Boolean hierarchy can be written out as JSON content.


<tt>ObjectMapper.readValue()</tt> must be called with a Map type and it will build a Map/List/String/Number/Boolean hierarchy:
=JSON to Java=
 
JSON to Java parsing is done by <tt>ObjectMapper.readValue()</tt>, which must be called with an Object.class type:
 
<syntaxhighlight lang='java'>
import com.fasterxml.jackson.databind.ObjectMapper;
 
ObjectMapper mapper = new ObjectMapper();
 
Object root = mapper.readValue(src, Object.class);


<pre>
if (root instance of Map) {
    ...
}
else if (root instanceof List) {
    ...
}
...
</syntaxhighlight>
 
This is the conversion table:
 
{|
| '''JSON Type''' || '''Java Type'''
|-
|  object || <tt>LinkedHashMap<String,Object></tt>
|-
| array || <tt>ArrayList<Object></tt>
|-
| string || <tt>String</tt>
|-
| number (no fraction) || <tt>Integer</tt>, <tt>Long</tt> or <tt>BigInteger</tt> (smallest applicable)
|-
| number (fraction) || <tt>Double</tt> (configurable to use <tt>BigDecimal</tt>)
|-
| true|false || <tt>Boolean</tt>
|-
| null || <tt>null</tt>
 
|}
 
It is possible to call readValue() with a more specific type. For example, if the top level JSON value is an Object, readValue() can be called upon a Map.
 
<syntaxhighlight lang='java'>
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;


ObjectMapper om = new ObjectMapper();
ObjectMapper mapper = new ObjectMapper();


Map root = on.readValue(inputStream, Map.class);
Map root = mapper.readValue(inputStream, Map.class);
</pre>
</syntaxhighlight>


Most generically:
However, if the top level JSON value is a List, the code above will throw an exception:


<pre>
<pre>
Object root = mapper.readValue(src, Object.class);
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
</pre>
</pre>
==JSON to Java Code Example==
{{External|[https://github.com/NovaOrdis/playground/blob/master/json/jackson/06-simple-data-binding-json-to-java/src/main/java/io/novaordis/playground/json/jackson/simpledatabinding/json2java/Main.java JSON to Java Simple Data Binding Example]}}
=Generics Support=


It is possible to enable generic type information (like <tt>Map<String, Object></tt>). For details, see [[Jackson Full Data Binding#generic_type_information|Full Data Binding]].
It is possible to enable generic type information (like <tt>Map<String, Object></tt>). For details, see [[Jackson Full Data Binding#generic_type_information|Full Data Binding]].


=JSON to Java=
=Java to JSON=
 
The Map/List/String/Number/Boolean hierarchy can be written out as JSON by <tt>ObjectMapper.writeValue()</tt>.
 
<syntaxhighlight lang='java'>
ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
ObjectMapper om = new ObjectMapper();


==JSON to Java Code Example==
Map root = ...


{{External|https://github.com/NovaOrdis/playground/blob/master/json/jackson/simple-data-binding-json-to-java/src/main/java/io/novaordis/playground/json/jackson/simpledatabinding/json2java/Main.java}}
om.writeValue(baos, root);


=Java to JSON=
System.out.println(new String(baos.toByteArray()));
</syntaxhighlight>


==Java to JSON Code Example==
==Java to JSON Code Example==
{{External|[https://github.com/NovaOrdis/playground/blob/master/json/jackson/05-simple-data-binding-java-to-json/src/main/java/io/novaordis/playground/json/jackson/simpledatabinding/java2json/Main.java Java to JSON Simple Data Binding Example]}}

Latest revision as of 00:32, 14 November 2018

Internal

Overview

Simple data binding extracts data from JSON and initializes an in-memory Java object hierarchy. Unlike the tree model, which uses JsonNodes, simple data binding assembles the hierarchy representing the JSON data out of Maps, Lists, Strings, Numbers, Booleans and nulls. A Map/List/String/Number/Boolean hierarchy can be written out as JSON content.

JSON to Java

JSON to Java parsing is done by ObjectMapper.readValue(), which must be called with an Object.class type:

import com.fasterxml.jackson.databind.ObjectMapper;

ObjectMapper mapper = new ObjectMapper();

Object root = mapper.readValue(src, Object.class);

if (root instance of Map) {
    ...
}
else if (root instanceof List) {
    ...
}
...

This is the conversion table:

JSON Type Java Type
object LinkedHashMap<String,Object>
array ArrayList<Object>
string String
number (no fraction) Integer, Long or BigInteger (smallest applicable)
number (fraction) Double (configurable to use BigDecimal)
false Boolean
null null

It is possible to call readValue() with a more specific type. For example, if the top level JSON value is an Object, readValue() can be called upon a Map.

import com.fasterxml.jackson.databind.ObjectMapper;

ObjectMapper mapper = new ObjectMapper();

Map root = mapper.readValue(inputStream, Map.class);

However, if the top level JSON value is a List, the code above will throw an exception:

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token

JSON to Java Code Example

JSON to Java Simple Data Binding Example

Generics Support

It is possible to enable generic type information (like Map<String, Object>). For details, see Full Data Binding.

Java to JSON

The Map/List/String/Number/Boolean hierarchy can be written out as JSON by ObjectMapper.writeValue().

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ObjectMapper om = new ObjectMapper();

Map root = ...

om.writeValue(baos, root);

System.out.println(new String(baos.toByteArray()));

Java to JSON Code Example

Java to JSON Simple Data Binding Example