Jackson Simple Data Binding: Difference between revisions
Jump to navigation
Jump to search
Line 5: | Line 5: | ||
=Overview= | =Overview= | ||
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 out of Maps, Lists, Strings, Numbers, Booleans and nulls | 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. | ||
=JSON to Java= | =JSON to Java= |
Revision as of 20:19, 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 a type corresponding to the top-level JSON data type, usually a Map.
type and it will build a Map/List/String/Number/Boolean hierarchy:
import com.fasterxml.jackson.databind.ObjectMapper; ObjectMapper om = new ObjectMapper(); Map root = on.readValue(inputStream, Map.class);
Most generically:
Object root = mapper.readValue(src, Object.class);
It is possible to enable generic type information (like Map<String, Object>). For details, see Full Data Binding.
JSON to Java Code Example
Java to JSON
ObjectMapper om = new ObjectMapper(); Map root = ... om.writeValue(baos, root);