Seephor Seephor - 1 month ago 16
Java Question

How to deserialize nested interface types with Gson

I am trying to get Gson to deserialize a nested object in my JSON into a class that implements an interface.

Here is my interface:

public interface ActivationFunction {

public float activate(float input);


I have an implementing class named LinearActivation and a Layer class that has a class variable of type ActivationFunction. Here is my JSON:

"layers" : [

I read this post: Polymorphism with gson and I searched Gson docs here:

but I can't find any documentation on creating a typeHierarchyAdapter. I followed the example in the first link, but I am not sure how INSTANCE and CLASSNAME should be used within my JSON structure.

Here is the type hierarchy adapter:

public class ActivationFunctionAdapter implements JsonDeserializer<ActivationFunction> {

private static final String CLASSNAME = "CLASSNAME";
private static final String INSTANCE = "INSTANCE";

public ActivationFunction deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
JsonPrimitive prim = (JsonPrimitive) jsonObject.get(CLASSNAME);
String className = prim.getAsString();

Class<?> klass = null;
try {
klass = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new JsonParseException(e.getMessage());
return context.deserialize(jsonObject.get(INSTANCE), klass);


Any help or guidance would be much appreciated


I was able to get this to work by following the guide here:

also same info can be seen in the answer by Perception in this question: Using Gson with Interface Types

Basically just had to add the RuntimeTypeAdapterFactory class to my project, (which the code is available in the extras package in the Gson github), then define a type token and use it. For example:

TypeToken<ActivationFunction> functionTypeToken = new TypeToken<ActivationFunction>() {};

RuntimeTypeAdapterFactory<ActivationFunction> typeFactory = 
RuntimeTypeAdapterFactory.of(ActivationFunction.class, "type")
                         .registerSubtype(LinearFunction.class, "linear");

final Gson gson = new GsonBuilder().registerTypeAdapterFactory(typeFactory).create();