Home >Java >javaTutorial >How to use the @Until annotation of the Gson library in Java?

How to use the @Until annotation of the Gson library in Java?

WBOY
WBOYforward
2023-09-01 11:41:101047browse

How to use the @Until annotation of the Gson library in Java?

@Until Note Can be used with the setVersion() method of the GsonBuilder class. This annotation can be applied to fields in java classes and accepts float as parameter. This parameter represents the version number of the field that has been serialized. @Until Notes Can manage the version control of JSON classes in Network Services.

Syntax

@Documented
@Retention(value=RUNTIME)
@Target(value={FIELD,TYPE})
public @interface Until

Example

import com.google.gson.annotations.Until;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GsonUntilAnnotationTest {
   public static void main(String[] args) {
      Employee emp = new Employee();
      emp.setEmployeeName("Adithya");
      emp.setEmployeeId(115);
      emp.setEmployeeTechnology("Python");
      emp.setEmploeeAddress("Pune");
      System.out.println("Using version 0.5");
      GsonBuilder gsonBuilder = new GsonBuilder();
      Gson gson = gsonBuilder.setPrettyPrinting().setVersion(0.5).create();
      String jsonString = gson.toJson(emp);
      System.out.println(jsonString);
      System.out.println("Using version 1.0");
      gsonBuilder = new GsonBuilder();
      gson = gsonBuilder.setPrettyPrinting().setVersion(1.0).create();
      jsonString = gson.toJson(emp);
      System.out.println(jsonString);
      System.out.println("Using version 1.1");
      gsonBuilder = new GsonBuilder();
      gson = gsonBuilder.setPrettyPrinting().setVersion(1.1).create();
      jsonString = gson.toJson(emp);
      System.out.println(jsonString);
   }
}
// Employee class
class Employee {
   private String empName;
   private int empId;
<strong>   </strong>@Until(1.1)
   private String empTech;
   @Until(1.1<strong>)</strong>
   private String empAddress;
   public String getEmployeeName() {
      return empName;
   }
   public void setEmployeeName(String empName) {
      this.empName = empName;
   }
   public int getEmployeeId() {
      return empId;
   }
   public void setEmployeeId(int empId) {
      this.empId = empId;
   }
   public String getEmployeeTechnology() {
      return empTech;
   }
  public void setEmployeeTechnology(String empTech) {
      this.empTech = empTech;
   }
   public String getEmploeeAddress() {
      return empAddress;
   }
   public void setEmploeeAddress(String empAddress) {
      this.empAddress = empAddress;
   }
}

Output

Using version 0.5
{
   "empName": "Adithya",
   "empId": 115,
   "empTech": "Python",
   "empAddress": "Pune"
}
Using version 1.0
{
   "empName": "Adithya",
   "empId": 115,
   "empTech": "Python",
   "empAddress": "Pune"
}
Using version 1.1<strong>
</strong>{
   "empName": "Adithya",
   "empId": 115
}

The above is the detailed content of How to use the @Until annotation of the Gson library in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete