In the last chapter, we discussed about the Jackson API including its overview, features and setup of our local environment using the Maven plugin and eclipse as IDE (Integrated Development Environment). In this tutorial, we are going to build our first Java based application using Jackson API. We are going to use the same environment which we had prepared in the last tutorial.
Example on Jackson API based application
In the following example, we have created an Employee class. The Employee class has five attributes , name, empId, designation, salary, grade, and age. Next, we have created a JSON string with Employee details. Firstly, we have conducted the deserialization of JSON string into an Employee object and then its serialization of Object back to a JSON string.
Employee.java
package com.eduonix.jackson_demo; /** * * @author Aparajita * */ public class Employee { private String name; private String empId; private String designation; private float salary; private String grade; private int age; public Employee() { } 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 getEmpId() { return empId; } public void setEmpId(String empId) { this.empId = empId; } public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } public String toString() { return "Employee [ name: " + name + ", empId: " + empId + ", designation: " + designation + ", grade: " + grade + ", salary: " + salary + ", age: " + age + " ]"; } }
JacksonDemo.java
package com.eduonix.jackson_demo; import java.io.IOException; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.SerializationConfig; /** * Aparajita * */ public class JacksonDemo { public static void main(String args[]) { ObjectMapper mapper = new ObjectMapper(); String jsonString = "{\"name\":\"Mahesh\", \"empId\":\"12345\", \"designation\":\"Software Developer\", \"salary\":\"1000000.00\", \"grade\":\"A4\", \"age\":21}"; // Mapping of JSON to Employee try { Employee employee = mapper.readValue(jsonString, Employee.class); System.out.println(employee); mapper.enable(SerializationConfig.Feature.INDENT_OUTPUT); jsonString = mapper.writeValueAsString(employee); System.out.println(jsonString); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Output: – When we execute the above JacksonDemo class as a Java Application, we can observe the following output.
Employee [ name: Mahesh, empId: 12345, designation: Software Developer, grade: A4, salary: 1000000.0, age: 21 ] { "name" : "Mahesh", "empId" : "12345", "designation" : "Software Developer", "salary" : 1000000.0, "grade" : "A4", "age" : 21 }
Explanation of the code
Step 1: – Creation of the ObjectMapper Object as shown below. It is a reusable object.
ObjectMapper mapper = new ObjectMapper();
Step 2: – Deserialization of the JSON to Object. Here, we have used readValue () method in order to procure the Object from the JSON. It accepts two parameters. They are the JSON string or the source of the JSON string and the object type (here Employee.class).
//JSON to Object Conversion Employee employee = mapper.readValue(jsonString, Employee.class);
Step 3: – Serialization of the Object back to JSON. Here, we have used writeValueAsString () method in order to get the JSON string representation of an object.
//Object to JSON Conversion jsonString = mapper.writeValueAsString(employee);
Source code to build our java based application using Jackson API
Conclusion: –
In this chapter, we have built a simple Jackson based Java application where we have use used the ObjectMapper class of Jackson API to conduct the deserialization of JSON to an Object (here Employee class object) and then its serialization back to a JSON string by using the appropriate methods as discussed before.