Custom Instance Creator Using Gson in Java



While parsing a JSON String to or from a Java object, by default, Gson tries to create an instance of the Java class by calling the default constructor. In the case of Java, if a class doesn't contain a default constructor or we want to do some initial configuration while creating Java objects, we need to create and register our own instance creator.

Custom Instance Creator Using Gson

Custom instance means creating a new instance that is not the default. In this instance, we can add whatever properties we want. There are many libraries that create custom instances. In this article, we will be using the Gson library to create a custom instance in Java.

Gson is a library that allows us to convert Java objects into JSON and JSON into Java objects. It is a very popular library for working with JSON in Java.

We can create a custom instance creator in Gson using the InstanceCreator interface and need to implement the createInstance(Type type) method.

To use the Gson library, we need to add it to our project. If you are using Maven, add this to your pom.xml file:

<dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>2.8.9</version>
</dependency>

If you do not use Maven, you can download the jar file from here.

Syntax of createInstance() Method

The syntax of the createInstance() method is as follows:

createInstance(Type type);

Steps

Following are the steps to create a custom instance creator using Gson in Java:

  • Import the Gson library.
  • Create a class Person with fields for name and age.
  • Create a class PersonInstanceCreator that implements the InstanceCreator interface.
  • Implement the createInstance() method to create a new instance of the Person class.
  • Create a GsonBuilder object.
  • Register the PersonInstanceCreator with the GsonBuilder.
  • Create a Gson object using the GsonBuilder.
  • Create a JSON string.
  • Convert the JSON string to a Person object using the fromJson() method of the Gson.
  • Print the Person object.

Example

Following is the code to create a custom instance creator using Gson in Java:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import java.lang.reflect.Type;

public class CustomInstanceCreatorExample {
   public static void main(String[] args) {
      // Create a JSON string
      String jsonString = "{"name":"Ansh","age":23}";
      // Create a GsonBuilder object
      GsonBuilder gsonBuilder = new GsonBuilder();
      // Register the PersonInstanceCreator with the GsonBuilder
      gsonBuilder.registerTypeAdapter(Person.class, new PersonInstanceCreator());
      // Create a Gson object using the GsonBuilder
      Gson gson = gsonBuilder.create();
      // Convert the JSON string to a Person object
      Person person = gson.fromJson(jsonString, Person.class);
      // Print the Person object
      System.out.println("Name: " + person.getName());
      System.out.println("Age: " + person.getAge());
   }
}
class Person {
   private String name;
   private int age;

   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }

   public String getName() {
      return name;
   }

   public int getAge() {
      return age;
   }
}
class PersonInstanceCreator implements InstanceCreator<Person> {
   @Override
   public Person createInstance(Type type) {
      // Create a new instance of the Person class
      return new Person("Default Name", 0);
   }
}

Output

Name: Ansh
Age: 23
Updated on: 2025-05-12T16:19:13+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements