Gson Custom Serialization Example

Sometimes default serialization/deserialization used by GSON is not sufficient enough and we need a custom behavior to be applied while serializing/deserializing objects to/from JSON. This can easily be done by writing custom JsonSerializer & JsonDeserializer implementation and hooking them with GsonBuilder.

We will write a custom implementation to handle a date property in our example pojo.

Step 1: Include GSON dependency in pom.xml

<project xmlns="" xmlns:xsi=""




Step 2: Sample POJO

Below pojo contains a date property named ‘promoDate’. We will write our custom implementation to provide the date for this property in a specific format(‘dd/MM/yyyy’).

package com.websystique.json.gson.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Car {

	private String name;
	private int model;
	private double price;
	private Date promoDate;
	private List<String> colors = new ArrayList<String>();

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public int getModel() {
		return model;

	public void setModel(int model) {
		this.model = model;

	public double getPrice() {
		return price;

	public void setPrice(double price) {
		this.price = price;

	public List<String> getColors() {
		return colors;

	public void setColors(List<String> colors) {
		this.colors = colors;

	public Date getPromoDate() {
		return promoDate;

	public void setPromoDate(Date promoDate) {
		this.promoDate = promoDate;

	public String toString() {
		return "Car [name=" + name + ", model=" + model + ", price=" + price
				+ ", promoDate=" + promoDate + ", colors=" + colors + "]";

Step 3: Write Custom Date Serializer to convert Java date to JSON string in particular format

package com.websystique.json.gson.model;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;


public class CustomDateSerializer 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));

Step 4: Write Custom Date Deserializer to convert date from JSON string to Java date

package com.websystique.json.gson.model;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class CustomDateDeserializer 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) {
		return null;

Step 5: Register custom Serializer/Deserializer with GsonBuilder, Run

package com.websystique.json.gson;

import java.util.Date;

import com.websystique.json.gson.model.Car;
import com.websystique.json.gson.model.CustomDateDeserializer;
import com.websystique.json.gson.model.CustomDateSerializer;

public class GsonCustomSerializationDemo {

	public static void main(String args[]){

		GsonBuilder gsonBuilder = new GsonBuilder().setPrettyPrinting();
		/* Register custom serializers with GsonBuilder */
		gsonBuilder.registerTypeAdapter(Date.class, new CustomDateSerializer());
		gsonBuilder.registerTypeAdapter(Date.class, new CustomDateDeserializer());
		Gson gson = gsonBuilder.create();  
		Car car = new Car();
		car.setPromoDate( new Date());
		String jsonString = gson.toJson(car); // Convert car object to JSON string
		Car car2 = gson.fromJson(jsonString, Car.class);// Convert JSON String to car object


Following is the output. You can see that promoDate follows the format mentioned via custom implementation.

  "name": "AUDI",
  "model": 2014,
  "price": 30000.0,
  "promoDate": "05/06/2015",
  "colors": [
Car [name=AUDI, model=2014, price=30000.0, promoDate=Fri Jun 05 00:00:00 CEST 2015, colors=[GRAY, BLACK, WHITE]]

That’s it. In the next post we will learn about Gson ExclusionStrategy used to exclude some fields from serializaion/deserialization.