您好,欢迎访问一九零五行业门户网

如何在Java中配置Gson以启用版本支持?

the gson library provides a simple versioning system for the java objects that it reads and writes and also provides an annotation named @since for the versioning concept @since(versionnumber).
we can create a gson instance with versioning using the gsonbuilder().setversion() method. if we mentioned like setversion(2.0), means that all the fields having 2.0 or less are eligible to parse.
syntaxpublic gsonbuilder setversion(double ignoreversionsafter)
exampleimport 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 classclass 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"}
以上就是如何在java中配置gson以启用版本支持?的详细内容。
其它类似信息

推荐信息