The ObjectMapper class is a fundamental component of the Jackson JSON library in Java. In the ever-evolving landscape of software development, efficient data exchange is a cornerstone of modern applications. The Jackson ObjectMapper class emerges as a vital tool in this endeavor, providing a bridge between Java objects and JSON data.
The ObjectMapper class serves a dual purpose. It’s used for both JSON serialization (converting Java objects to JSON) and JSON deserialization (converting JSON back to Java objects). Here’s a beginner-friendly example that shows how to use the ObjectMapper
class for both serialization and deserialization:
Table of Contents
Step 1: Set Up Your Environment
Before you start, ensure you have the Jackson library added to your project’s dependencies in the pom.xml
file. In your Maven project, open the pom.xml
file. Inside the <dependencies>
section, add the following dependency for Jackson:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
Note: Maven dependency “jackson-databind” uses the “jackson-core” and “jackson-annotations” internally so you don’t need to add them separately. Adding only “jackson-databind” dependency is enough.
Now, save the pom.xml
file. Maven will automatically download the Jackson library. See the complete environment setup of Jackson Library in Eclipse for the Maven project, here.
Step 2: Create the Employee Class
Create an Employee
class with attributes like id, name, and department as follows:
import com.fasterxml.jackson.annotation.JsonProperty;
public class Employee {
@JsonProperty("empId")
private int id;
@JsonProperty("empName")
private String name;
private String department;
// getters, and setters method
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
Step 3: Serialization – Convert Java Object to JSON
Now, let’s create a program to serialize an Employee
object to JSON using the ObjectMapper
:
import com.fasterxml.jackson.databind.ObjectMapper;
public class ObjectMapperSerializationExample {
public static void main(String[] args) throws Exception {
// Create an Employee object
Employee employee = new Employee();
employee.setId(1);
employee.setName("John Doe");
employee.setDepartment("IT");
// Create an ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
// Serialize the Employee object to JSON
String json = objectMapper.writeValueAsString(employee);
// Print the JSON
System.out.println("Serialized JSON:");
System.out.println(json);
}
}
This program creates an Employee
object, uses the ObjectMapper
to serialize it to JSON, and then print the JSON representation.
Step 4: Deserialization – Convert JSON to Java Object
Now, let’s write a program to deserialize JSON back into an Employee
object using the ObjectMapper
:
import com.fasterxml.jackson.databind.ObjectMapper;
public class ObjectMapperDeserializationExample {
public static void main(String[] args) throws Exception {
// JSON data representing an Employee
String json = "{\"empId\":1,\"empName\":\"John Doe\",\"department\":\"IT\"}";
// Create an ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
// Deserialize JSON to an Employee object
Employee employee = objectMapper.readValue(json, Employee.class);
// Access and print fields of the Employee object
System.out.println("Deserialized Employee:");
System.out.println("Employee ID: " + employee.getId());
System.out.println("Employee Name: " + employee.getName());
System.out.println("Employee Department: " + employee.getDepartment());
}
}
This program takes a JSON string, uses the ObjectMapper
to deserialize it into an Employee
object, and then prints the object’s fields.
Step 5: Running the Application
Compile and run both programs. You should see the following output:
Serialization Output:
Serialized JSON:
{“department”:”IT”,”empId”:1,”empName”:”John Doe”}
Deserialization Output:
Deserialized Employee:
Employee ID: 1
Employee Name: John Doe
Employee Department: IT
Conclusion
By following these steps, you’ve created a Java application that demonstrates how to use the ObjectMapper
class from the Jackson library for both JSON serialization and deserialization. This knowledge is essential for working with JSON data in Java applications. These foundational skills are paramount for anyone working with JSON data in Java applications, and the ObjectMapper
class in Jackson makes the process a breeze.
References:
Jackson ObjectMapper
Jackson Documentation