Java Google Json (Gson) Custom Serializer

Just as we saw in the previous tutorial, Gson provides way to specify custom serializers and deserializers. Register a custom serializer with the GsonBuilder if you need you own way to convert a java object to json and you a custom deserializer if you dont like Gson’s way of converting json to the java object. The first example below shows a custom serializer and the second example shows a custom deserializer.

Custom Serializer

Create a custom serializer by implementing a com.studytrails.json.gson.JsonSerializer and implementing the public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context); method. src is the source object and Type is the type of the source object. The example below demonstrates a custom Serializer.


package com.studytrails.json.gson;

import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;

public class DogSerializer implements JsonSerializer {
	@Override
	public JsonElement serialize(Dog src, Type typeOfSrc, JsonSerializationContext context) {
		// This method gets involved whenever the parser encounters the Dog
		// object (for which this serializer is registered)
		JsonObject object = new JsonObject();
		String name = src.getName().replaceAll(" ", "_");
		object.addProperty("name", name);
		// we create the json object for the dog and send it back to the
		// Gson serializer
		return object;
	}

	public static void main(String[] args) {
		Animall<Dog> animal = new Animall<Dog>();
		Dog dog = new Dog("I am a dog");
		animal.setAnimal(dog);
		// Create the GsonBuilder and register a serializer for the Dog class.
		// Whenever the Dog class is encountered Gson calls the DogSerializer
		// we set pretty printing own to format the json
		Gson gson = new GsonBuilder().registerTypeAdapter(Dog.class, new DogSerializer()).setPrettyPrinting().create();
		// Since Animal contains generic type create the type using TypeToken
		// class.
		Type animalType = new TypeToken<Animal<Dog>>() {
		}.getType();
		System.out.println(gson.toJson(animal, animalType));
	}
}





		
		


The Animal class

package com.studytrails.json.gson;

public class Animal {

	public T animal;

	public void setAnimal(T animal) {
		this.animal = animal;
	}

	public T get() {
		return animal;
	}

}




		
		


The Dog class

package com.studytrails.json.gson;

public class Dog {
	private String name;

	public Dog(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

}

Custom DeSerializer

Use a custome De serializer to create a Dog Object from the json. To create a deserializer implement the
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException;
method.

	package com.studytrails.json.gson;

import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;

public class DogDeserialiser implements JsonDeserializer<Dog> {
	@Override
	public Dog deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		String name = json.getAsJsonObject().get("name").getAsString();
		name = name.replace(" ", "_");
		Dog dog = new Dog(name);

		return dog;
	}

	public static void main(String[] args) {
		String json = "{\"animal\":{\"name\":\"I am a dog\"}}";
		Gson gson = new GsonBuilder().registerTypeAdapter(Dog.class, new DogDeserialiser()).create();
		Type animalType = new TypeToken<Animal<Dog>>() {
		}.getType();
		Animal<Dog> animal = gson.fromJson(json, animalType);
		System.out.println(animal.get().getName());
	}

}
	
	

Leave a Comment