Full Jackson data binding with specialized objects

If the JSON data format, which a client receives, is well-structured, you can directly map the content to a concrete Java class. A full data binding solution fits well in such a scenario. For instance, you can create an Employee class for representing the employee data presented in the JSON format as long as the JSON content structure does not change. The following example uses the data binding offering from Jackson to convert the JSON content from the emp.json file into an Employee object. This example calls readValue(InputStream src, Class<T> valueType) on ObjectMapper to get the Java representation for the JSON content:

// emp.json file has following contents: 
//{"employeeId":100,"firstName":"John","lastName":"Chen"}
ObjectMapper objectMapper = new ObjectMapper();
Employee employee = objectMapper.readValue(new File("emp.json"),
Employee.class);

The next example demonstrates how you can create a Java collection containing the Employee objects from a JSON array of employees. Here, we need to construct a Java collection type, indicating the type of elements in the collection. To create a collection type, you can call the constructCollectionType() method on the com.fasterxml.jackson.databind.type.TypeFactory instance returned by ObjectMapper:

ObjectMapper objectMapper = new ObjectMapper();  
CollectionType collectionType =
objectMapper.getTypeFactory().constructCollectionType
(List.class, Employee.class);
//"emp-array.json" file contains JSON array of employee data
List<Employee> emp = objectMapper.readValue(new File
("emp-array.json"), collectionType);

To convert a Java object into the JSON representation, you can call the writeValue(OutputStream out, Object value) method on ObjectMapper. The writeValue() method serializes any Java value to JSON and writes it to the output stream present in the method call. The following code snippet converts the employee object into the JSON structure and writes the content to the emp.json file:

//Get the employee object 
Employee employee = getEmployeeEntity();
//Convert the object in to JSON and write to a file
objectMapper.writeValue(new File("emp.json"), employee);
How does Jackson map JSON object values to a Java class?

The default mapping mechanism used by Jackson is based on the bean naming properties. The binding layer copies the matching properties from the source to the destination. This implies that all the names present in a JSON object need to match the Java class properties for the default mapping mechanism to work. However, you can override the default mapping behavior by annotating a desired field (or by the getter and setter methods) with @JsonProperty. This annotation is used to override the default property name that is used during the serialization and deserialization process. To learn more, visit http://wiki.fasterxml.com/JacksonAnnotations.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.109.234