Google Gson Interview Questions and Answers
Question: How to custom Serialization and De-serialization in Gson?Answer:Many times, we need to write/read the JSON values which are not default representation of java object. In that case, we need to write custom serializer and deserializer of that java type. In our example, I am writing serializer and deserializer for java.util.Date class, which will help writing the Date format in "dd/MM/yyyy" format. DateSerializer.java import java.lang.reflect.Type; import java.text.SimpleDateFormat; import java.util.Date; import com.google.gson.JsonElement; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; public class DateSerializer implements JsonSerializer<Date> { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); public JsonElement serialize(Date date, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(dateFormat.format(date)); } } DateDeserializer.java import java.lang.reflect.Type; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; public class DateDeserializer implements JsonDeserializer<Date> { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); public Date deserialize(JsonElement dateStr, Type typeOfSrc, JsonDeserializationContext context) { try { return dateFormat.parse(dateStr.getAsString()); } catch (ParseException e) { e.printStackTrace(); } return null; } } Now you can register these serializer and deserializer with GsonBuilder as below: GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer()); gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer()); Complete example of serializer and deserializer is as below: Employee employee = new Employee(); employee.setId(1); employee.setFirstName("Arindam"); employee.setLastName("Ghosh"); employee.setRoles(Arrays.asList("FINANCE", "MANAGER")); employee.setBirthDate(new Date()); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer()); gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer()); Gson gson = gsonBuilder.create(); //Convert to JSON System.out.println(gson.toJson(employee)); //Convert to java objects System.out.println(gson.fromJson("{'id':1,'firstName':'Arindam','lastName':'Ghosh','roles':['FINANCE','MANAGER'],'birthDate':'17/06/2014'}", Employee.class)); Output: { "id":1, "firstName":"Bob", "lastName":"Richardson", "roles":["FINANCE","MANAGER"], "birthDate":"17/06/2014" } Employee [id=1, firstName=Arindam, lastName=Ghosh, roles=[FINANCE, MANAGER], birthDate=Tue Jun 17 00:00:00 IST 2014] |
Is it helpful?
Yes
No
Most helpful rated by users:
- What is Google-Gson?
- How to convert Java objects to JSON format?
- What are the two ways to create Gson objects?
- What is Instance Creator? Why and when do we require this?
- How to custom Serialization and De-serialization in Gson?