Creating a custom serializer or deserializer

In this post under GSon, I will explain with example how to create custom serializer and deserializer and configure Gson object to use it.

For our example lets take the below JavaBean Class structure.

Student

package defaultPackage;

import java.util.Date;

public class Student {
private int id;
private String name;
private Integer rollnum;
private Date date;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getRollnum() {
return rollnum;
}
public void setRollnum(Integer rollnum) {
this.rollnum = rollnum;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}

@Override
public String toString() {
return "{id:" + id +", name:" + name + ", date:" + date + "}";
}
}

If we serialize the above JavaBean class we get the below output

    {"id":1,"name":"name1","rollnum":100,"date":"Feb 23, 2024, 8:43:11 AM"}

If we want change the way date is serialized, we can use custom serializer and deserializer.

To achieve this, we need to create one serializer class and one deserializer clas as shown below

Date Serializer

package defaultPackage;

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

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

public class DateSerializer implements JsonSerializer<Date> {
@Override
public JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) {
return new JsonPrimitive(date.getTime());
}
}

Date DeSerializer

package defaultPackage;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

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

public class DateDeSerializer implements JsonDeserializer<Date> {
@Override
public Date deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
return new Date(jsonElement.getAsLong());
}
}

In the DateSerializer class we serialize the date as a long value and in DateDeSerializer class we deserialize the long value to date.

Now lets see how to use both the classes in the Main class

Main class

1  package defaultPackage;
2
3 import com.google.gson.Gson;
4 import com.google.gson.GsonBuilder;
5
6 import java.util.Date;
7
8 public class GsonDemo17 {
9 public static void main(String[] args) {
10 Gson gson = new Gson();
11 Student student = new Student();
12 student.setId(1);
13 student.setName("name1");
14 student.setRollnum(100);
15 student.setDate(new Date());
16 String result = gson.toJson(student);
17 System.out.println("Before configuring the custom serializer/deserializer");
18 System.out.println(result);
19
20 GsonBuilder gsonBuilder = new GsonBuilder();
21 gsonBuilder = gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer());
22 gsonBuilder = gsonBuilder.registerTypeAdapter(Date.class, new DateDeSerializer());
23 gson = gsonBuilder.create();
24 System.out.println("After configuring the custom serializer/deserializer");
25 result = gson.toJson(student);
26 System.out.println(result);
27 student = null;
28 student = gson.fromJson(result, Student.class);
29 System.out.println("Result of deserialization");
30 System.out.println(student);
31 }
32 }

To configure custom serializer and deserializer we need to create Gson instance using the GsonBuilder class as shown at line 20.

At line 21 and 22, we register the serializer and deserializer class by calling “registerTypeAdapter” method availabe on “GsonBuilder” instance.

We pass two arguments, first being the class type for which the serializer and deserializer has to be called and second being the actual serializer/deserializer instance.

In this way, we can use custom serializer and deserializer

Below is the output

Output

    Before configuring the custom serializer/deserializer
{"id":1,"name":"name1","rollnum":100,"date":"Feb 23, 2024, 8:43:11 AM"}
After configuring the custom serializer/deserializer, the serializer output will be
{"id":1,"name":"name1","rollnum":100,"date":1708657991840}
Result of deserialization
{id:1, name:name1, date:Fri Feb 23 08:43:11 IST 2024}

Leave a comment