Without Book Interview Questions and Answers | Online Test | Moct Test
Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Institute Training Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

Google Gson Interview Questions and Answers

Ques. What is Versioning Support in Gson?
Ans.
This is excellent feature you can use, if the class file you are working has been modified in different versions and fields has been annotated with @Since. All you need to do is to use setVersion() method of GsonBuilder.

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer());
gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
//Specify the version like this
gsonBuilder.setVersion(1.0);
Gson gson = gsonBuilder.create();

Fields added in various versions in Employee.java
public class Employee
{
   @Since(1.0)
   private Integer id;
   private String firstName;
   private String lastName;
    
   @Since(1.1)
   private List<String> roles;
    
   @Since(1.2)
   private Date birthDate;
    
   //Setters and Getters
}

Now test the version feature:
//Using version 1.0 fields
gsonBuilder.setVersion(1.0);
 
Output:
{"id":1,"firstName":"Arindam","lastName":"Ghosh"}
  
//Using version 1.1 fields
gsonBuilder.setVersion(1.1);
 
Output:
{"id":1,"firstName":"Arindam","lastName":"Ghosh","roles":["FINANCE","MANAGER"]}
  
//Using version 1.2 fields
gsonBuilder.setVersion(1.2);
 
Output:
{"id":1,"firstName":"Arindam","lastName":"Ghosh","roles":["FINANCE","MANAGER"],"birthDate":"17/06/2014"}
Is it helpful? Yes No

Most helpful rated by users:

©2016 WithoutBook