Custom Serialization and Deserialization

GSON offers the possibility to plug in custom serializers and deserializers. our custom serializers can convert Java values to custom JSON, and deserializers can convert custom JSON to Java values again.

Custom Serializer

A custom serializer in GSON has to implement the JsonSerializer interface. The JsonSerializer interface looks like this:

public interface JsonSerializer<T> {
    public JsonElement serialize(T value, Type type,
        JsonSerializationContext jsonSerializationContext) {
    }
}

Implementing a custom serializer that can serializer boolean values will looks like this:

public class BooleanSerializer implements JsonSerializer<Boolean> {

  public JsonElement serialize(Boolean aBoolean, Type type,
    JsonSerializationContext jsonSerializationContext) {
    if(aBoolean){
       return new JsonPrimitive(1);
    }
      return new JsonPrimitive(0);
  }
}

Observe the T type parameter is replaced with the Boolean class in two places.
Inside the serialize() method we can convert the value (a Boolean in this cas) to a JsonElement.

In the above example we use a JsonPrimitive which is also a JsonElement. As you we see, boolean values of true are converted to 1 and false to 0, instead of true and false normally used in JSON.
Registering this custom serializer is done like this:

GsonBuilder builder = new GsonBuilder();

builder.registerTypeAdapter(Boolean.class, new BooleanSerializer()) ;

Gson gson = builder.create();

Call to registerTypeAdapter() which registers the customer serializer with GSON.
Once registered, the Gson instance created from the GsonBuilder will use the custom serializer.

Check This Pojo

public class PojoWithBoolean {

    public String username = null;
    public Boolean isSuperUser = false;
}

Here is how serializing a PojoWithBoolean instance looks:

PojoWithBoolean pojo = new PojoWithBoolean();
pojo.username = "abc";
pojo.isSuperUser = false;

String pojoJson = gson.toJson(pojo);

System.out.println(pojoJson);

The output printed from this example would be:
{"username":"abc","isSuperUser":0}
Notice how the false value of isSuperUser is converted to a 0.

Custom Deserializer

GSON also provides support for custom deserializers. A custom deserializer must implement the JsonDeserializer interface. The JsonDeserializer interface looks like this:

public interface JsonDeserializer<T> {
    public Boolean deserialize(JsonElement jsonElement, 
        Type type, JsonDeserializationContext jsonDeserializationContext) 
        throws JsonParseException;
}

Implementing a custom deserializer for the Boolean type would look like this:

public class BooleanDeserializer implements JsonDeserializer<Boolean> {

    public Boolean deserialize(JsonElement jsonElement, Type type,
    JsonDeserializationContext jsonDeserializationContext)
    throws JsonParseException {

        return jsonElement.getAsInt() == 0 ? false : true;
    }
}

Registering the custom deserializer with GSON is done using another version of the registerTypeConverter() method.

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Boolean.class, new BooleanDeserializer());

Gson gson = builder.create();

And here is how parsing a JSON string with the created Gson instance looks:


String jsonSource = "{\"username\":\"abc\",\"isSuperUser\":1}";

PojoWithBoolean pojo = gson.fromJson(jsonSource, PojoWithBoolean.class);

System.out.println(pojo.isSuperUser);

The output printed from this GSON custom deserializer example would be:
true
since the 1 in the JSON string would be converted to the boolean value true .