Maybe I'm running in the wrong direction, but I have a list of elements which I want to read.
I have an abstract base class let's call it Person
:
public abstract class Person {
public int id;
public String name;
}
Now I have two possible implementations:
public class Hunter implements Person {
public int skill;
// and some more stuff
}
public class Zombie implements Person {
public int uglyness;
// and some more stuff
}
Now I have this example JSON:
[
{"id":1, "type":"zombie", "name":"Ugly Tom", "uglyness":42},
{"id":2, "type":"hunter", "name":"Shoot in leg Joe", "skill":0}
]
How can I read this JSON as List<Person>
?
I'm playing for a while with TypeAdapterFactory
and tried to use a class called CustomizedTypeAdapterFactory
since my real structure is a little more complex as the funny example above.
I ended in that I want to delegate the serialization with this call:
return gson.getDelegateAdapter(this, resultType);
However I have no idea how I can create at runtime that TypeToken<T>
which is required for this call. Any ideas?