How to configure Gson to enable versioning support in Java?



Enabling version support means that the Gson library will be able to handle multiple versions of the same class. We use this when we have a class that has been modified and we want to be able to deserialize JSON data that was created using an older version of the class.

Enabling Versioning Support in Java Gson Library

The Gson library provides a simple versioning system for the Java objects that it reads and writes, and also provides an annotation named @Since for the versioning concept @Since(versionnumber).

We can create a Gson instance with versioning using the GsonBuilder().setVersion() method. If we mentioned like setVersion(2.0), it means that all the fields having 2.0 or less are eligible to parse.

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 are not using Maven, you can download the jar file from here. Following are the steps to enable version support in Gson:

  • Create a class Person with fields for name and age. Annotate the field with @Since(versionnumber). or @Until(versionnumber).
  • Create an instance of the GsonBuilder class. Now, set the version using the setVersion() method of the GsonBuilder.
  • Create a Gson object using builder.create().
  • Now, create a Person object. Convert the Person object to a JSON string using the toJson().
  • Modify the object. For example, change the age of the person.
  • Convert the modified Person object to a JSON string using the toJson().
  • Deserialize the JSON string back to a Person object using the fromJson().
  • Print the deserialized Person object.

Example

Following is the code to enable version support in Gson:

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

class Person {
   @Since(1.0)
   String name;

   @Since(1.1)
   int age;

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

public class GsonVer {
   public static void main(String[] args) {
      Person p1 = new Person("Ansh", 25);

      Gson g1 = new GsonBuilder().setVersion(1.0).create();
      String j1 = g1.toJson(p1);
      System.out.println("Ver 1.0 JSON: " + j1);

      Gson g2 = new GsonBuilder().setVersion(1.1).create();
      String j2 = g2.toJson(p1);
      System.out.println("Ver 1.1 JSON: " + j2);

      Person p2 = g2.fromJson(j2, Person.class);
      System.out.println("Deserialized: " + p2.name + ", " + p2.age);
   }
}

Output

Ver 1.0 JSON: {"name":"Ansh"}
Ver 1.1 JSON: {"name":"Ansh","age":25}
Deserialized: Ansh, 25
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-05-13T15:35:43+05:30

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements