GSON - Version Support Example

In this quick article, we will discuss how to use @Since annotation to support multiple versions of the same object. GSON introduced the @Since annotation to support multiple versions of the same object. We can use this annotation on Classes and Fields. 
In order to enable this feature, we need to register a version number to use when serializing/deserializing. If no version number is set on the Gson instance, all the fields will be serialized/deserialized regardless of the version. 
Gson Versioning basically allows you to hide or add fields of your classes to support multiple versions.

GSON Maven Dependency

To use Gson with Maven2/3, you can use the Gson version available in Maven Central by adding the following dependency:
<dependencies>
    <!--  Gson: Java to Json conversion -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.5</version>
      <scope>compile</scope>
    </dependency>
</dependencies>

Gson Versioning Support Example

The @Since annotation is used to annotate your classes or fields with a version number. This annotation takes a double as argument indicating the version number from when this field is available.
class User {

    @Since(1.0)
    private int id;

    private String firstName;
    private String lastName;

    @Since(1.1)
    private int age;

    @Since(1.2)
    private String gender;

    @Since(1.3)
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int i) {
        this.id = i;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + ", gender=" +
            gender + "]";
    }
}
When no version number is specified in the Gson instance, all fields are serialized/deserialized. When a version number is specified, all fields that match that version number are used
package net.javaguides.gson;

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

/**
 * 
 * @author Ramesh Fadatare
 *
 */
public class GSONVersionSupport {
    public static void main(String[] args) {
        User user = new User();
        user.setFirstName("Ramesh");
        user.setLastName("Fadatare");
        user.setGender("Male");
        user.setAge(28);
        user.setId(100);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String result = gson.toJson(user);
        System.out.println("Without version: " + result);

        gson = new GsonBuilder().setVersion(1.0).setPrettyPrinting().create();
        result = gson.toJson(user);
        System.out.println("Version 1.0: " + result);

        gson = new GsonBuilder().setVersion(1.1).setPrettyPrinting().create();
        result = gson.toJson(user);
        System.out.println("Version 1.1: " + result);

        gson = new GsonBuilder().setVersion(1.2).setPrettyPrinting().create();
        result = gson.toJson(user);
        System.out.println("Version 1.2: " + result);

        gson = new GsonBuilder().setVersion(1.3).setPrettyPrinting().create();
        result = gson.toJson(user);
        System.out.println("Version 1.3: " + result);
    }
}
Output:
Without version: {
  "id": 100,
  "firstName": "Ramesh",
  "lastName": "Fadatare",
  "age": 28,
  "gender": "Male"
}
Version 1.0: {
  "id": 100,
  "firstName": "Ramesh",
  "lastName": "Fadatare"
}
Version 1.1: {
  "id": 100,
  "firstName": "Ramesh",
  "lastName": "Fadatare",
  "age": 28
}
Version 1.2: {
  "id": 100,
  "firstName": "Ramesh",
  "lastName": "Fadatare",
  "age": 28,
  "gender": "Male"
}
Version 1.3: {
  "id": 100,
  "firstName": "Ramesh",
  "lastName": "Fadatare",
  "age": 28,
  "gender": "Male"
}

Complete Example for Reference

package net.javaguides.gson;

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

/**
 * 
 * @author Ramesh Fadatare
 *
 */
public class GSONVersionSupport {
    public static void main(String[] args) {
        User user = new User();
        user.setFirstName("Ramesh");
        user.setLastName("Fadatare");
        user.setGender("Male");
        user.setAge(28);
        user.setId(100);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String result = gson.toJson(user);
        System.out.println("Without version: " + result);

        gson = new GsonBuilder().setVersion(1.0).setPrettyPrinting().create();
        result = gson.toJson(user);
        System.out.println("Version 1.0: " + result);

        gson = new GsonBuilder().setVersion(1.1).setPrettyPrinting().create();
        result = gson.toJson(user);
        System.out.println("Version 1.1: " + result);

        gson = new GsonBuilder().setVersion(1.2).setPrettyPrinting().create();
        result = gson.toJson(user);
        System.out.println("Version 1.2: " + result);

        gson = new GsonBuilder().setVersion(1.3).setPrettyPrinting().create();
        result = gson.toJson(user);
        System.out.println("Version 1.3: " + result);
    }
}

class User {

    @Since(1.0)
    private int id;

    private String firstName;
    private String lastName;

    @Since(1.1)
    private int age;

    @Since(1.2)
    private String gender;

    @Since(1.3)
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int i) {
        this.id = i;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + ", gender=" +
            gender + "]";
    }
}
Output:
Without version: {
  "id": 100,
  "firstName": "Ramesh",
  "lastName": "Fadatare",
  "age": 28,
  "gender": "Male"
}
Version 1.0: {
  "id": 100,
  "firstName": "Ramesh",
  "lastName": "Fadatare"
}
Version 1.1: {
  "id": 100,
  "firstName": "Ramesh",
  "lastName": "Fadatare",
  "age": 28
}
Version 1.2: {
  "id": 100,
  "firstName": "Ramesh",
  "lastName": "Fadatare",
  "age": 28,
  "gender": "Male"
}
Version 1.3: {
  "id": 100,
  "firstName": "Ramesh",
  "lastName": "Fadatare",
  "age": 28,
  "gender": "Male"
}

Reference

Comments