Java Jackson @JsonPOJOBuilder Example

1. Introduction

The Jackson library offers a rich set of annotations for controlling the serialization and deserialization of Java objects. One such annotation is @JsonPOJOBuilder, which comes in handy when working with the Builder pattern. This annotation allows Jackson to understand and use the builder pattern for deserialization, providing a seamless way to create immutable or complex objects from JSON data.

2. Example Steps

1. Add the required Jackson library dependencies to your project.

2. Create a Java class (POJO) using the Builder pattern.

3. Annotate the builder class with @JsonPOJOBuilder and its associated methods.

4. Use Jackson's ObjectMapper to deserialize JSON data into the POJO.

5. Display the resulting object.

3. Java Jackson @JsonPOJOBuilder Example

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;

public class JsonPOJOBuilderExample {

    public static void main(String[] args) {
        String jsonData = "{\"name\":\"John\",\"age\":30}";

        try {
            ObjectMapper mapper = new ObjectMapper();
            Person person = mapper.readValue(jsonData, Person.class);
            System.out.println(person);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @JsonDeserialize(builder = Person.Builder.class)
    static class Person {
        private final String name;
        private final int age;

        private Person(Builder builder) {
            this.name = builder.name;
            this.age = builder.age;
        }

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

        @JsonPOJOBuilder(withPrefix = "set")
        static class Builder {
            private String name;
            private int age;

            public Builder setName(String name) {
                this.name = name;
                return this;
            }

            public Builder setAge(int age) {
                this.age = age;
                return this;
            }

            public Person build() {
                return new Person(this);
            }
        }
    }
}

Output:

Person [name=John, age=30]

4. Step By Step Explanation

1. We start by defining a Person class, which is structured using the Builder pattern. The Person class is immutable, with all its fields being final.

2. The @JsonDeserialize annotation on the Person class points Jackson to the Builder class to use for deserialization.

3. Inside the Builder class, we annotate it with @JsonPOJOBuilder. The withPrefix attribute is set to "set", indicating that our setter methods in the builder have the "set" prefix.

4. The Builder class contains setter methods for each field, and these methods return the Builder object, allowing for method chaining.

5. The build() method in the Builder class is responsible for producing the Person object.

6. In the main method, we use Jackson's ObjectMapper to deserialize a JSON string into a Person object and then print it.

7. Jackson utilizes the annotated Builder class to handle the deserialization process, creating a new Person object using the builder.

With the combination of @JsonDeserialize and @JsonPOJOBuilder, Jackson can easily integrate with the Builder pattern, ensuring the correct construction of complex or immutable objects during deserialization.


Comments