Using JsonbCreator annotation

In this post under JSONB, I will explain the purpose of JsonbCreator annotation with an example.

Whenever we deserialize a json data to java object, it is required that the Class of the object should have default/no argument constructor, so that JSONB runtime can create an instance of the class with the help of the constructor and set the properties.

As a result even if add a custom constructor or factory method that creates the instance, JSONB runtime doesn’t consider them.

JsonbCreator annotation solves the above problem.

If we annotate a custom constructor or factory method with the annotation, then from that point onwards JSONB runtime will use the custom constructor or factory method to create instances.

If custom constructor or factory method has parameters then they must be annotated with JsonbProperty annotation.

If the class has multiple custom constructor or factory methods only one of them has to be annotated with JsonbCreator annotation.

Below is an example

Book


1  import javax.json.bind.annotation.JsonbCreator;
2  import javax.json.bind.annotation.JsonbProperty;
3  
4  public class Book {
5   private String title;
6   private String author;
7   private int price;
8   
9   public Book() {
10      
11  }
12  
13  @JsonbCreator
14  public Book(@JsonbProperty("title") String title, @JsonbProperty("author") String author, @JsonbProperty("price") int price) {
15      System.out.println("Custom Constructor");
16      this.title = title;
17      this.author = author;
18      this.price = price;
19  }
20  
21  public String getTitle() {
22      return title;
23  }
24  public void setTitle(String title) {
25      this.title = title;
26  }
27  public String getAuthor() {
28      return author;
29  }
30  public void setAuthor(String author) {
31      this.author = author;
32  }
33  public int getPrice() {
34      return price;
35  }
36  public void setPrice(int price) {
37      this.price = price;
38  }
39  
40  @Override
41  public String toString() {
42      return this.title + ":" + author + ":" + price;
43  }
44 }

At line 14 to 19, we have created a custom constructor and at line 13 we annotated it with JsonbCreator. The parameters of the custom constructor are also annotated with JsonbProperty.

Now when the object’s json format is deserialized, the custom constructor is used to create the instance.

Below is the main class code

Main Code


import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;

public class JsonbDemo12 {
    public static void main(String[] args) {
        JsonbConfig jsonbConfig = new JsonbConfig();
        jsonbConfig.withFormatting(true);
        Jsonb jsonb = JsonbBuilder.create(jsonbConfig);
        
        Book book1 = new Book();
        book1.setTitle("Title1");
        book1.setAuthor("Author1");
        book1.setPrice(1000);
        
        String result = jsonb.toJson(book1);
        System.out.println(result);
        
        Book book2 = jsonb.fromJson(result, Book.class);
        System.out.println(book2);
    }
}

Output

{
“author”: “Author1”,
“price”: 1000,
“title”: “Title1”
}
Custom Constructor
Title1:Author1:1000

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s