How to deserialize a JSON string using @JsonCreator annotation in Java?

The @JsonProperty annotation can be used to indicate the property name in JSON. This annotation can be used for a constructor or factory method. The @JsonCreator annotation is useful in situations where the @JsonSetter annotation cannot be used. For instance, immutable objects do not have any setter methods, so they need their initial values injected into the constructor.

@JsonProperty - Constructor

Example

import com.fasterxml.jackson.annotation.*;
import java.io.IOException;
import com.fasterxml.jackson.databind.*;
public class JsonCreatorTest1 {
   public static void main(String[] args) throws IOException {
      <strong>ObjectMapper </strong>om = new <strong>ObjectMapper()</strong>;
      String jsonString = "{"id":"101", "fullname":"Ravi Chandra", "location":"Pune"}";
      System.out.println("JSON: " + jsonString);
      Customer customer = om.<strong>readValue</strong>(jsonString, Customer.class);
      System.out.println(customer);
   }
}
<strong>// Customer class</strong>
class Customer {
   private String id;
   private String name;
   private String address;
   public Customer() {
   }
<strong>   @JsonCreator
</strong>   public Customer(
<strong>      @JsonProperty("id")</strong> String id,
<strong>      @JsonProperty("fullname")</strong> String name,  
<strong>      @JsonProperty("location")</strong> String address) {
      this.id = id;
      this.name = name;
      this.address = address;
   }
<strong>   @Override
</strong>   public String toString() {
      return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]";
   }
}

Output

JSON: <strong>{"id":"101", "fullname":"Ravi Chandra", "location":"Pune"}</strong>
<strong>Customer [id=101, name=Ravi Chandra, address=Pune]</strong>


@JsonCreator - Factory Method

Example

import com.fasterxml.jackson.annotation.*;
import java.io.IOException;
import com.fasterxml.jackson.databind.*;
public class JsonCreatorTest2 {
   public static void main(String[] args) throws IOException {
      <strong>ObjectMapper</strong> mapper = new <strong>ObjectMapper()</strong>;
      String jsonString = "{"id":"102", "fullname":"Raja Ramesh",          "location":"Hyderabad"}";
      System.out.println("JSON: " + jsonString);
      Customer customer = mapper.<strong>readValue</strong>(jsonString, Customer.class);
      System.out.println(customer);
   }
}
<strong>// Customer class</strong>
class Customer {
   private String id;
   private String name;
   private String address;
   public Customer() {
   }
<strong>   @JsonCreator
</strong>   public static Customer createCustomer(
      <strong>@JsonProperty("id")</strong> String id,
<strong>      @JsonProperty("fullname")</strong> String name,
<strong>   @JsonProperty("location")</strong> String address) {
      Customer customer = new Customer();
      customer.id = id;
      customer.name = name;
      customer.address = address;
      return customer;
   }
<strong>   @Override
</strong>   public String toString() {
         return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]";
   }
}

Output

JSON: <strong>{"id":"101", "fullname":"Raja Ramesh", "location":"Hyderabad"}</strong>
<strong>Customer [id=102, name=Raja Ramesh, address=Hyderabad]</strong>
Updated on: 2020-02-17T08:18:20+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements