我有一个包含两个泛型列表的数据类:
data class Warehouse(
val cars: MutableList<out Car>,
val planes: MutableList<out Plane>,
)
目前,我尝试使用以下方法序列化对象:
val warehouse = Warehouse(cars, planes)
val json = Gson().toJson(warehouse)
为我提供以下json:
{
"cars": [
{}
],
"planes": [
{}
],
}
如果我使用序列化汽车
val cars: MutableList<Car> = getCars()
val json = Gson().toJson(cars)
一切正常,即json包含正确的信息。
根据文档,已知类型的对象可以包含任何通用类型的字段:
/**
* 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}.
*/
我在这里想念什么?
答案 0 :(得分:0)
解决方案是手动注册其他类型的适配器:
val json = GsonBuilder()
.registerTypeAdapter(Car::class.java, CarSerializer())
.registerTypeAdapter(Plane::class.java, PlaneSerializer())
.create()
.toJson(data)
其中CarSerializer
定义为:
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}")
}
}
}
PlaneSerializer
的定义方法相同。