>  기사  >  Java  >  Java에서 Gson 라이브러리의 @Until 주석을 사용하는 방법은 무엇입니까?

Java에서 Gson 라이브러리의 @Until 주석을 사용하는 방법은 무엇입니까?

WBOY
WBOY앞으로
2023-09-01 11:41:10976검색

Java에서 Gson 라이브러리의 @Until 주석을 사용하는 방법은 무엇입니까?

@Until 주석 GsonBuilder 클래스의 setVersion() 메서드와 함께 사용할 수 있습니다. 이 주석은 Java 클래스의 필드에 적용될 수 있으며 부동 소수점을 매개변수로 허용합니다. 이 매개변수는 필드가 직렬화된 버전 번호를 나타냅니다. @Until 주석네트워크 서비스에서 JSON 클래스의 버전 제어를 관리할 수 있습니다.

구문

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

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;
   }
}

출력

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
}

위 내용은 Java에서 Gson 라이브러리의 @Until 주석을 사용하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제