Java Jackson @JsonCreator Example

1. Introduction

Jackson provides an extensive set of annotations to control the serialization and deserialization of Java objects. One such powerful annotation is @JsonCreator, which tells the Jackson library how to construct an object instance from JSON. Especially useful for immutable objects or those without default constructors, this annotation marks a constructor or a static factory method to be used for deserialization.

2. Example Steps

1. Create a Person class with immutable fields and no default constructor.

2. Use the @JsonCreator annotation to mark a constructor and use @JsonProperty to bind JSON properties to constructor parameters.

3. Serialize and deserialize a Person object to demonstrate the functionality of the annotation.

3. Java Jackson @JsonCreator Example

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonJsonCreatorExample {

    public static void main(String[] args) throws Exception {
        // Initialize ObjectMapper
        ObjectMapper mapper = new ObjectMapper();

        // JSON string to be deserialized
        String jsonString = "{\"name\":\"John\",\"age\":30}";

        // Deserialize the JSON to a Person object
        Person person = mapper.readValue(jsonString, Person.class);

        // Print the details of the deserialized person
        System.out.println(person.toString());
    }
}

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

    @JsonCreator
    public Person(@JsonProperty("name") String name, @JsonProperty("age") int age) {
        this.name = name;
        this.age = age;
    }

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

Output:

Person [name=John, age=30]

4. Step By Step Explanation

The @JsonCreator annotation is used to signal Jackson that the annotated constructor or static factory method should be used for creating an instance of the associated class. When dealing with immutable objects or objects without default constructors, this annotation is crucial for successful deserialization.

In our example, the Person class has no default constructor, and its fields are final. To deserialize a Person object from a JSON representation, Jackson needs to know which constructor to use and how to map the JSON properties to the constructor's parameters. The @JsonCreator annotation tells Jackson to use the annotated constructor, while the @JsonProperty annotations on the constructor's parameters map the corresponding JSON properties to the constructor's parameters.

In the provided code, when the Person object is deserialized, Jackson recognizes the @JsonCreator annotation on the Person constructor and uses it to create a new Person instance using the provided JSON data. The name and age JSON properties are mapped to the constructor's parameters using the @JsonProperty annotations, allowing for the successful creation of the Person object from the JSON data.


Comments