问题
For some unknown reason if I have:
class A{
int stars;
public int getStars(){
return stars;
}
public void setStarts(int stars){
this.stars = stars;
}
}
class B extends A{
int sunshines;
[getter and setter for sunshines]
}
class C{
List<A> classes;
[get and set for classes]
}
if I serialize an Object of type C I have only the fields of A in the serialized objects in the field classes (while I would expect to have the fields of B if the object is a B).
How to do that?
回答1:
I've tried your example and replacing List<A>
with List<Object>
in class C solved this problem.
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<Object>();
c.classes.add(new A());
c.classes.add(new B());
System.out.println(gson.toJson(c));
}
public static class A {
int stars;
}
public static class B extends A {
int sunshines;
}
public static class C {
List<Object> classes;
}
}
Produces:
{
"classes": [
{
"stars": 0
},
{
"sunshines": 0,
"stars": 0
}
]
}
This, however, prevent you from using default json deserialization... Consider refactoring you OOD and inheritance relationships.
回答2:
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
}
]
}
回答3:
I used Ivan's answer to clue me into a slightly more elegant solution - it's not perfect, but it worked well enough for me. I created a new JsonSerializer that throws away the type of A by casting it to an Object. It seems that when given a plain Object Gson looks at the actual type of the object instead of referring from the type of the variable that's passed in.
public static class ASerializer implements JsonSerializer<A> {
@Override
public JsonElement serialize( A in, Type type, JsonSerializationContext ctx ) {
return ctx.serialize( (Object) in );
}
}
You then pass the ASerializer
to a GsonBuilder
to get yourself a Gson instance to use:
GsonBuilder gb = new GsonBuilder();
gb.registerTypeAdapter( A.class, new ASerializer() );
Gson gson = gb.create();
回答4:
Gson support for polymorphic type handling is incomplete.
If possible, I highly recommend switching to Jackson, which handles the serialization problem in the original question flawlessly.
If Gson must be used, then custom serialization processing to handle such a data structure is necessary. Fortunately, the implementation of the custom serializer can be quite simple. For example:
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class GsonFoo
{
public static void main(String[] args)
{
C c = new C();
c.classes = new ArrayList<A>();
c.classes.add(new A(42));
c.classes.add(new B(8));
System.out.println(new Gson().toJson(c));
// output:
// {"classes":[{"stars":42},{"stars":-1}]}
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(A.class, new ASerializer());
Gson gson = gsonBuilder.create();
System.out.println(gson.toJson(c));
// output:
// {"classes":[{"stars":42},{"sunshines":8,"stars":-1}]}
}
}
class ASerializer implements JsonSerializer<A>
{
@Override
public JsonElement serialize(A src, Type typeOfSrc, JsonSerializationContext context)
{
Gson gson = new Gson();
return gson.toJsonTree(src);
}
}
class A
{
public int stars;
A(int s)
{
stars = s;
}
}
class B extends A
{
public int sunshines;
B(int s)
{
super(-1);
sunshines = s;
}
}
class C
{
public List<A> classes;
}
来源:https://stackoverflow.com/questions/8153582/gson-doesnt-serialize-fields-defined-in-subclasses