InstanceCreator example

In this post under Gson, I will show with example the purpose of “InstanceCreator” interface provided by Gson framework.

Whenever we define a Pojo class, we don’t add constructor of our own, as we use Java provided default no argument constructor to create an object of the class.

But sometimes we end up adding constructors with arguments. In those cases the default no argument constructor is not added by the Java.

Gson when deserializing Pojo classes uses the default no argument constructor to create instances and then it sets the instance fields.

But when Pojo classes have custom constructor Gson fails to deserialize those classes as there is no default no argument constructor.

To help Gson in those case, “InstanceCreator” interface has been provided.

For our example lets consider the below Pojo class

Book

package defaultPackage;

public class Book {
private String title;
private String author;
private int price;

public Book(String title, String author, int price) {
//super();
this.title = title;
this.author = author;
this.price = price;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("title: ").append(title).append(";");
sb.append("author: ").append(author).append(";");
sb.append("price: ").append(price);
return sb.toString();
}
}

As shown in the above code, “Book” class has three fields “title”, “author”, and “price”. It also has getter and setter method for each field and a “toString” method.

We can serialize the “Book” class using the below code snippet

    String result = gson.toJson(book);

But we cannot deserialize using the below code snippet

    Book book = gson.fromJson(result, Book.class);

The above code snippet won’t work as it requires default no argument constructor which is not there in the “Book” class.

So the solution is to use the “InstanceCreator” interface as shown below. I will create a class that implements the interface.

BookInstanceCreator

1  package defaultPackage;
2
3 import java.lang.reflect.Type;
4
5 import com.google.gson.InstanceCreator;
6
7 public class BookInstanceCreator implements InstanceCreator<Book> {
8 @Override
9 public Book createInstance(Type type) {
10 Book book = new Book("", "", 0);
11 return book;
12 }
13 }

In the above code, I created a class “BookInstanceCreator” which implements “InstanceCreator” interface.

Then I provide an implementation of “createInstance” abstract method.

Our implementation of the method will create a “Book” instance with empty values and return it.

Next I will use this to deserialize the “Book” class as shown below

1)     BookInstanceCreator bookInstanceCreator = new BookInstanceCreator();
2) gson = gsonBuilder.registerTypeAdapter(Book.class, bookInstanceCreator).create();
3) book = gson.fromJson(result, Book.class);

Where “gsonBuilder” is an instance of “GsonBuilder” class.

First at line 1, we create an instance of “BookInstanceCreator” class named “bookInstanceCreator”

Second when creating an instance of “Gson” class using “gsonBuilder” instance, we register the “bookInstanceCreator” instance by calling “registerTypeAdapter” method on “gsonBuilder” instance.

This is the way to inform Gson framework to use the “bookInstanceCreator” instance when deserializing the data.

Next at line 3, we deserialize the String data stored in “result” variable in usual way.

Below is the complete main code for your reference

Main class

package defaultPackage;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonDemo12 {
public static void main(String[] args) {
Book book = new Book("title1", "author1", 100);
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
String result = gson.toJson(book);
System.out.println("Serialization result: " + result);
BookInstanceCreator bookInstanceCreator = new BookInstanceCreator();
gson = gsonBuilder.registerTypeAdapter(Book.class, bookInstanceCreator).create();
book = gson.fromJson(result, Book.class);
System.out.println("Deserialization result with InstanceCreator: " + book);
}
}

Below is the output

Output

Serialization result: {"title":"title1","author":"author1","price":100}
Deserialization result with InstanceCreator: title: title1;author: author1;price: 100

Leave a comment