Java Jackson Serialize and Deserialize Example

1. Introduction

Jackson is a powerful Java library used to process JSON data format. It provides simple methods to convert between Java objects and JSON and vice versa. In this post, we'll explore the basic use of Jackson to serialize a Java object into JSON and to deserialize a JSON string back into a Java object.

2. Example Steps

1. Add the Jackson library dependencies to your project.

2. Create a Java class (POJO) for the data structure.

3. Serialize the Java object to a JSON string.

4. Deserialize the JSON string back to a Java object.

5. Display both the serialized and deserialized data for confirmation.

3. Java Jackson Serialize and Deserialize Example

import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonSerializeDeserializeExample {

    public static void main(String[] args) {
        try {
            // Step 2: Create a Java object
            Person person = new Person();
            person.setName("Alice");
            person.setAge(30);

            // Step 3: Serialize Java object to JSON string
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonString = objectMapper.writeValueAsString(person);
            System.out.println("Serialized JSON: " + jsonString);

            // Step 4: Deserialize JSON string back to Java object
            Person deserializedPerson = objectMapper.readValue(jsonString, Person.class);
            System.out.println("Deserialized Java Object: " + deserializedPerson.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static class Person {
        private String name;
        private int age;

        // Getters and setters
        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; }

        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + "]";
        }
    }
}

Output:

Serialized JSON: {"name":"Alice","age":30}
Deserialized Java Object: Person [name=Alice, age=30]

4. Step By Step Explanation

1. The Person class is our data structure with fields name and age. It also has a toString() method for easy representation.

2. We instantiate the Person class and populate it with sample data for name and age.

3. An ObjectMapper instance is created which is central to the serialization and deserialization process in Jackson.

4. Using the writeValueAsString method of ObjectMapper, we serialize our Person object into a JSON string.

5. Using the readValue method of ObjectMapper, we deserialize the JSON string back into a Person object.

6. The console output confirms that the serialization and deserialization processes worked correctly by showing us the original Java object and its JSON representation.

With Jackson, converting between Java objects and JSON has never been easier. By understanding the core methods provided by ObjectMapper, developers can efficiently process JSON in their applications.


Comments