Gson 2.1 supports this out of the box:
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GsonInheritanceTest
{
public static void main(String[] args)
{
Gson gson = new GsonBuilder().setPrettyPrinting().create();
C c = new C();
c.classes = new ArrayList<A>();
c.classes.add(new A(1));
c.classes.add(new B(2, 3));
System.out.println(gson.toJson(c));
}
static class A
{
int stars;
A(int stars)
{
this.stars = stars;
}
}
static class B extends A
{
int sunshines;
B(int stars, int sunshines)
{
super(stars);
this.sunshines = sunshines;
}
}
static class C
{
List<A> classes;
}
}
The ouput is
{
"classes": [
{
"stars": 1
},
{
"sunshines": 3,
"stars": 2
}
]
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…