6

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?

gotch4
  • 13,093
  • 29
  • 107
  • 170
  • 1
    Possible duplicate: http://stackoverflow.com/questions/5800433/polymorphism-with-gson – Kevin Nov 16 '11 at 15:19
  • Here you can find a working example: [http://stackoverflow.com/a/22081826/3315914](http://stackoverflow.com/a/22081826/3315914) – rpax Feb 28 '14 at 08:08

4 Answers4

9

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
    }
  ]
}
Guillaume Perrot
  • 4,278
  • 3
  • 27
  • 37
  • Not sure if it's the same problem, I'm using GSON v 2.8 and it fails in similar case https://stackoverflow.com/questions/65918374/why-gson-fails-to-convert-object-when-its-a-field-of-another-object – Alex Craft Jan 27 '21 at 18:29
5

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();
Volker Neumann
  • 476
  • 5
  • 6
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.

Ivan Sopov
  • 2,300
  • 4
  • 21
  • 39
  • hm... this way works, anyway, my java code won't work like this. – gotch4 Nov 16 '11 at 15:16
  • Thanks, but it's not a good solution 1) loosing type with using `List` 2) affecting the design with minor stuff like JSON serialisation, you shouldn't change the design you think is the best for your application, only because some serialisation library expect a different one. – Alex Craft Jan 27 '21 at 18:36
1

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;
}
Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97