Jackson Full Data Binding: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
(12 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
=Overview= | =Overview= | ||
Full data binding deserializes JSON into a custom Java type, using reflection to "fill out" the state. | |||
=JSON to Java= | =JSON to Java= | ||
< | <syntaxhighlight lang='java'> | ||
import com.fasterxml.jackson.databind.ObjectMapper; | import com.fasterxml.jackson.databind.ObjectMapper; | ||
Line 15: | Line 17: | ||
Root root = mapper.readValue(is, Root.class); | Root root = mapper.readValue(is, Root.class); | ||
</ | </syntaxhighlight> | ||
This is a generic conversion method, assuming that the JSON content matches the internal structure of the type <T>: | This is a generic conversion method, assuming that the JSON content matches the internal structure of the type <T>: | ||
< | <syntaxhighlight lang='java'> | ||
public static <T> T fromJson(String json, Class<? extends T> c) throws JsonConversionException { | public static <T> T fromJson(String json, Class<? extends T> c) throws JsonConversionException { | ||
Line 30: | Line 32: | ||
} | } | ||
} | } | ||
</ | </syntaxhighlight> | ||
=Java to JSON= | =Java to JSON= | ||
Line 40: | Line 42: | ||
Example: | Example: | ||
< | <syntaxhighlight lang='java'> | ||
import com.fasterxml.jackson.databind.ObjectMapper; | import com.fasterxml.jackson.databind.ObjectMapper; | ||
Line 49: | Line 51: | ||
mapper.writeValue(baos, root); | mapper.writeValue(baos, root); | ||
System.out.println(new String(baos.toByteArray())); | System.out.println(new String(baos.toByteArray())); | ||
</ | </syntaxhighlight> | ||
=Code | =Code Examples= | ||
{{External|[https://github.com/NovaOrdis/playground/blob/master/json/jackson/full-data-binding/src/main/java/io/novaordis/playground/json/jackson/fulldatabinding/Main.java Full Data Binding Example]}} | {{External|[https://github.com/NovaOrdis/playground/blob/master/json/jackson/07-full-data-binding/src/main/java/io/novaordis/playground/json/jackson/fulldatabinding/Main.java Full Data Binding Example]}} | ||
{{External|[https://github.com/ovidiuf/playground/tree/master/json/jackson/07.1-full-data-binding-variants Full Data Binding Example 2]}} | |||
=Generics Handling= | =Generics Handling= | ||
<span id="generic_type_information"></span>To enable generic type information (like "Map<String,Object>"), you have to use TypeReference container | <span id="generic_type_information"></span>To enable generic type information (like "Map<String,Object>"), you have to use TypeReference container. See http://wiki.fasterxml.com/JacksonDataBinding. | ||
{{External|[https://github.com/NovaOrdis/playground/blob/master/json/jackson/08-full-data-binding-and-generics/src/main/java/io/novaordis/playground/json/jackson/fulldatabinding/Main.java Full Data Binding and Generics Example]}} | |||
=Annotations= | |||
{{Internal|Jackson Annotations|Jackson Annotations}} | |||
=Abstract Class Handling= | |||
{{Internal|Jackson Full Data Binding and Abstract Class Handling|Abstract Class Handling}} | |||
=Custom Deserializers= | |||
{{Internal|Jackson Full Data Binding and Custom Deserializers|Custom Deserializers}} |
Latest revision as of 21:50, 14 November 2018
Internal
Overview
Full data binding deserializes JSON into a custom Java type, using reflection to "fill out" the state.
JSON to Java
import com.fasterxml.jackson.databind.ObjectMapper;
InputStream is = ...;
ObjectMapper mapper = new ObjectMapper();
Root root = mapper.readValue(is, Root.class);
This is a generic conversion method, assuming that the JSON content matches the internal structure of the type <T>:
public static <T> T fromJson(String json, Class<? extends T> c) throws JsonConversionException {
try {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(new ByteArrayInputStream(json.getBytes()), c);
}
catch (Exception e) {
throw new JsonConversionException(e);
}
}
Java to JSON
ObjectMapper will introspect a Java object coded according to the Java Beans conventions and output it as JSON content.
The method names (without the "get") will be used as field names.
Example:
import com.fasterxml.jackson.databind.ObjectMapper;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectMapper mapper = new ObjectMapper();
Root root = ... // some hierarchical object
mapper.writeValue(baos, root);
System.out.println(new String(baos.toByteArray()));
Code Examples
Generics Handling
To enable generic type information (like "Map<String,Object>"), you have to use TypeReference container. See http://wiki.fasterxml.com/JacksonDataBinding.