Gson 라이브러리는 간단한 버전 관리 시스템을 제공합니다. 읽고 쓰고 @Since라는 주석도 제공하는 Java 개체의 경우 버전 관리 개념 @Since(versionnumber) .
GsonBuilder().setVersion() 을 사용하여 버전 관리를 통해 Gson 인스턴스를 만들 수 있습니다. 방법. setVersion(2.0), 과 같이 언급한 경우 2.0 이하의 모든 필드를 구문 분석할 수 있음을 의미합니다.
구문
public GsonBuilder setVersion(double ignoreVersionsAfter)
예시
import com.google.gson.*;
import com.google.gson.annotations.*;
public class VersionSupportTest {
public static void main(String[] args) {
Person person = new Person();
person.firstName = "Raja";
person.lastName = "Ramesh";
Gson gson1 = new GsonBuilder().setVersion(1.0).setPrettyPrinting().create();
System.out.println("Version 1.0:");
System.out.println(gson1.toJson(person));
Gson gson2 = new GsonBuilder().setVersion(2.0).setPrettyPrinting().create();
System.out.println("Version 2.0:");
System.out.println(gson2.toJson(person));
}
}
// Person class
class Person {
@Since(1.0)
public String firstName;
@Since(2.0)
public String lastName;
} 출력
Version 1.0:
{
"firstName": "Raja"
}
Version 2.0:
{
"firstName": "Raja",
"lastName": "Ramesh"
}