0

I've a data class containing two lists of generics:

data class Warehouse(
    val cars: MutableList<out Car>,
    val planes: MutableList<out Plane>,
)

For now, I tried to serialize my object using:

val warehouse = Warehouse(cars, planes)
val json = Gson().toJson(warehouse)

which gives me the following json:

{
    "cars": [
    {}
  ],
    "planes": [
    {}
  ],
}

If I serialize the cars using

val cars: MutableList<Car> = getCars()
val json = Gson().toJson(cars)

everything works as intended i.e. the json contains the right informations.

According to the documentation, an object of a known type can contain any field of generic type:

 /**
   * This method serializes the specified object into its equivalent Json representation.
   * This method should be used when the specified object is not a generic type. This method uses
   * {@link Class#getClass()} to get the type for the specified object, but the
   * {@code getClass()} loses the generic type information because of the Type Erasure feature
   * of Java. Note that this method works fine if the any of the object fields are of generic type,
   * just the object itself should not be of a generic type. If the object is of generic type, use
   * {@link #toJson(Object, Type)} instead. If you want to write out the object to a
   * {@link Writer}, use {@link #toJson(Object, Appendable)} instead.
   *
   * @param src the object for which Json representation is to be created setting for Gson
   * @return Json representation of {@code src}.
   */

What am I missing here?

Benjamin
  • 7,055
  • 6
  • 40
  • 60

1 Answers1

0

The solution is to register the different type adapter manually:

val json = GsonBuilder()
            .registerTypeAdapter(Car::class.java, CarSerializer())
            .registerTypeAdapter(Plane::class.java, PlaneSerializer())
            .create()
            .toJson(data)

with CarSerializer defined as:

class CarSerializer : JsonSerializer<Car> {

    override fun serialize(src: Car, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
        return when (src) {
            is Ferrari -> context.serialize(src, Ferrari::class.java)
            is Mercedes -> context.serialize(src, Mercedes::class.java)
            else -> throw IllegalArgumentException("Unspecified class serializer for ${src.javaClass.name}")
        }
    }
}

The PlaneSerializer is defined the same way.

Benjamin
  • 7,055
  • 6
  • 40
  • 60