是否可以序列化这些多态抽象类的列表?我想使用这样的东西:
Json.stringify(PowerStrategy.serializer.list, entities)
抽象类
@Polymorphic
abstract class PowerStrategy{
companion object{
val serializer by lazy {
SerializersModule {
polymorphic(PowerStrategy::class) {
BlockOptimizer::class with BlockOptimizer.serializer()
}
}
}
}
abstract val from: LocalTime
abstract val to: LocalTime
abstract fun applyToPowerPlan(plan: DayPowerPlan)
}
PowerStrategy
的众多实现之一
@SerialName("block_optimizer")
@Serializable
data class BlockOptimizer(
@ContextualSerialization
override val from: LocalTime,
@ContextualSerialization
override val to: LocalTime,
val blockLength: Int,
val numberOfBlocks: Int,
var weights:Array<Double> = Array(4*24){1.0}
) : PowerStrategy() {...}
如果我需要自定义列表序列化程序,请提供一个模式。