您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何在Java中使用Gson库的@Until注解?

2024/3/30 11:40:09发布24次查看
@until 注释可以与 gsonbuilder 类的 setversion() 方法一起使用。此注释可以应用于 java 类中的字段并接受 float 作为参数。此参数表示字段已序列化的版本号。 @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 classclass 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}
以上就是如何在java中使用gson库的@until注解?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product