Hi, is there a way for me to serialize a property ...
# serialization
a
Hi, is there a way for me to serialize a property named
type
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.
1
Here is an example code :
Copy code
@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.