
@Until 注释可以与 GsonBuilder 类的 setVersion() 方法一起使用。此注释可以应用于 java 类中的字段并接受 float 作为参数。此参数表示字段已序列化的版本号。 @Until 注释可以管理网络服务中 JSON 类的版本控制。
@Documented
@Retention(value=RUNTIME)
@Target(value={FIELD,TYPE})
public @interface Untilimport 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中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号