द Gson पुस्तकालय एक सरल संस्करण प्रणाली प्रदान करता है जावा ऑब्जेक्ट के लिए जिसे वह पढ़ता और लिखता है और @Since . नामक एक एनोटेशन भी प्रदान करता है संस्करण अवधारणा के लिए @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"
}