Jackson Simple Data Binding: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 9: Line 9:
=JSON to Java=
=JSON to Java=


JSON to Java parsing is done by <tt>ObjectMapper.readValue()</tt>, which must be called with a type corresponding to the top-level JSON data type, usually a Map.
JSON to Java parsing is done by <tt>ObjectMapper.readValue()</tt>, which must be called with an Object.class type:


<pre>
<pre>
Line 16: Line 16:
ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper = new ObjectMapper();


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


However, if the top level JSON value is a List, the code above will throw an exception:
if (root instance of Map) {
 
    ...
<pre>
}
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
else if (root instanceof List) {
    ...
}
...
</pre>
</pre>


That is why is best if the parsing starts with an Object:
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.


<pre>
<pre>
Line 32: Line 34:
ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper = new ObjectMapper();


Object root = mapper.readValue(src, Object.class);
Map root = mapper.readValue(inputStream, Map.class);
</pre>
 
However, if the top level JSON value is a List, the code above will throw an exception:


if (root instance of Map) {
<pre>
    ...
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
}
else if (root instanceof List) {
    ...
}
...
</pre>
</pre>



Revision as of 20:28, 26 February 2017

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) {
    ...
}
...

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

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

JSON to Java Code Example

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

Java to JSON

ObjectMapper om = new ObjectMapper();

Map root = ...

om.writeValue(baos, root);

Java to JSON Code Example