The data binding API is used for JSON conversion and accessing using attributes or using annotation POJO (normal Java object). Here are two types of it.
Simple Data Binding - Convert JSON from Java Maps, Lists, Strings, Numbers, Booleans and null objects.
Full Data Binding - Convert JSON to any JAVA type. We will bind separately in the next chapter.
ObjectMapper reads/writes JSON two types of data binding. The most convenient way to data binding is to XML-like JAXB parser.
Simple data binding
Simple data binding refers to JSON mapping to Java core data types. The following table lists the relationship between JSON type and Java type.
Let's take a look at simple data manipulation binding. Here we will map JAVA primitive types directly JSON and vice versa.
Create a Java class file named JacksonTester in directory C:/> Jackson_WORKSPACE.
File: JacksonTester.java
import java.io.File;import java.io.IOException;import java.util.HashMap;import java.util.Map;import org.codehaus.jackson.JsonGenerationException;import org.codehaus.jackson.JsonParseException;import org.codehaus.jackson.map.JsonMappingException;import org.codehaus.jackson.map.ObjectMapper;public class JacksonTester { public static void main(String args[]){ JacksonTester tester = new JacksonTester(); try { ObjectMapper mapper = new ObjectMapper(); Map<String,Object> studentDataMap = new HashMap<String,Object>(); int[] marks = {1,2,3}; Student student = new Student(); student.setAge(10); student.setName("Mahesh"); // JAVA Object studentDataMap.put("student", student); // JAVA String studentDataMap.put("name", "Mahesh Kumar"); // JAVA Boolean studentDataMap.put("verified", Boolean.FALSE); // Array studentDataMap.put("marks", marks); mapper.writeValue(new File("student.json"), studentDataMap); //result student.json //{ // "student":{"name":"Mahesh","age":10}, // "marks":[1,2,3], // "verified":false, // "name":"Mahesh Kumar" //} studentDataMap = mapper.readValue(new File("student.json"), Map.class); System.out.println(studentDataMap.get("student")); System.out.println(studentDataMap.get("name")); System.out.println(studentDataMap.get("verified")); System.out.println(studentDataMap.get("marks")); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}class Student { private String name; private int age; public Student(){} public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString(){ return "Student [ name: "+name+", age: "+ age+ " ]"; } }Verification results
Use javac to compile the following class:
The code copy is as follows:
C:/Jackson_WORKSPACE>javac JacksonTester.java
Now run jacksonTester to see the result:
The code copy is as follows:
C:/Jackson_WORKSPACE>java JacksonTester
Verify the output result
{name=Mahesh, age=10}Mahesh Kumarfalse[1, 2, 3] Full data binding
Full data binding refers to JSON mapping to any Java object.
//Create an ObjectMapper instanceObjectMapper mapper = new ObjectMapper(); //map JSON content to Student objectStudent student = mapper.readValue(new File("student.json"), Student.class);//map Student object to JSON contentmapper.writeValue(new File("student.json"), student);Let's take a look at simple data manipulation binding. Here we will map Java objects directly to JSON and vice versa.
Create a directory called JacksonTester in the Java class file C:/> Jackson_WORKSPACE.
File: JacksonTester.java
import java.io.File;import java.io.IOException;import org.codehaus.jackson.JsonGenerationException;import org.codehaus.jackson.JsonParseException;import org.codehaus.jackson.map.JsonMappingException;import org.codehaus.jackson.map.ObjectMapper;public class JacksonTester { public static void main(String args[]){ JacksonTester tester = new JacksonTester(); try { Student student = new Student(); student.setAge(10); student.setName("Mahesh"); tester.writeJSON(student); Student student1 = tester.readJSON(); System.out.println(student1); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void writeJSON(Student student) throws JsonGenerationException, JsonMappingException, IOException{ ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(new File("student.json"), student); } private Student readJSON() throws JsonParseException, JsonMappingException, IOException{ ObjectMapper mapper = new ObjectMapper(); Student student = mapper.readValue(new File("student.json"), Student.class); return student; }}class Student { private String name; private int age; public Student(){} public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString(){ return "Student [ name: "+name+", age: "+ age+ " ]"; } }Verification results
Use javac to compile the following class:
The code copy is as follows:
C:/Jackson_WORKSPACE>javac JacksonTester.java
Now run jacksonTester to see the result:
The code copy is as follows:
C:/Jackson_WORKSPACE>java JacksonTester
Verify that the copy code is copied as follows:
Student [ name: Mahesh, age: 10 ]