I have a tree object in JSON format I\'m trying to deserialize with Gson. Each node contains its child nodes as fields of object type Node. Node is an interface, which has
I'd suggest adding a custom JsonDeserializer for Node
s:
Gson gson = new GsonBuilder()
.registerTypeAdapter(Node.class, new NodeDeserializer())
.create();
You will be able to access the JsonElement
representing the node in the deserializer's method, convert that to a JsonObject
, and retrieve the field that specifies the type. You can then create an instance of the correct type of Node
based on that.