java中until方法_如何在Java中使用Gson库使用@Until批注?

@Until注释可以与使用setVersion()的方法GsonBuilder类。该注释可以应用于java类中的字段,并接受float作为参数。此参数表示该字段已序列化的版本号。该@Until注释可以管理JSON类的版本的Web服务。

语法@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;   @Until(1.1)

private String empTech;

@Until(1.1)   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{

"empName": "Adithya",

"empId": 115

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值