
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to configure Gson to enable versioning support in Java?
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.
Syntax
public GsonBuilder setVersion(double ignoreVersionsAfter)
Example
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; }
Output
Version 1.0: { "firstName": "Raja" } Version 2.0: { "firstName": "Raja", "lastName": "Ramesh" }
- Related Articles
- How to implement custom FieldNamingStrategy using Gson in Java?
- How to implement custom JsonAdapter using Gson in Java?
- How to convert Java array or ArrayList to JsonArray using Gson in Java?
- How to parse a JSON to Gson Tree Model in Java?
- How to implement custom JSON serialization with Gson in Java?
- How to enable webview java script in android?
- How to convert Java object to JSON using GSON library?
- How to exclude a field in Gson during serialization in Java?
- How to rename the properties of JSON using Gson in Java?
- How to pretty print JSON using the Gson library in Java?
- How to format a date using the Gson library in Java?
- How to serialize a null field using Gson library in Java?
- How to use @Until annotation using the Gson library in Java?
- How to parse a JSON without duplicate keys using Gson in Java?
- How to resolve "Expected BEGIN_OBJECT but was BEGIN_ARRAY" using Gson in Java?

Advertisements