Ayfri
04/23/2023, 9:56 AMtype
with polymorphic serialization ? I already changed classDiscriminator
to __type__
but the property is still not serialized, even when setting a custom SerialName
or JsonNames
on the property, it still get skipped.Ayfri
04/23/2023, 2:46 PM@Serializable
sealed interface RecipeType {
abstract val type: MyEnum
}
@Serializable
data class Blasting(var result: String) : RecipeType {
override val type = MyEnum.BLASTING
}
@Serializable
data class Crafting(var result: String) : RecipeType {
override val type = MyEnum.CRAFTING
}
I found what was the problem, it's because of encodeDefaults
setting. When it's turned off, as I set a default value for type
property in my subclasses, it will skip it.
So the solution is either using @EncodeDefault
on the type
property of each subclasses, or changing the encodeDefaults
setting, but it could introduce some problems. If anyone has a similar problem I encourage you to also set explicitNulls
to false to avoid some nulls being serialized even when encodeDefaults
is true.