Java Google Json (Gson) Serializing Generics

Serializing list

As we saw in the previous tutorial serializing and deserializing classes with generic types is non trivial since generic type information is lost while serializing. Gson provides a class called com.google.gson.reflect.TypeToken to store generic types. The example below shows how to use the TypeToken class to serialize and deserialize Classes with generic types.

Animal class

Dog Class

Leave a Reply

Your email address will not be published. Required fields are marked *