kotlinx.serialization
kotlinx.serialization copied to clipboard
Deserializing, Transitient field use default value instead of the values from subclass constructor
Describe the bug
When deserializing, Transit field of parent class use its default value instead of the value from subclass constructor
To Reproduce
@Serializable
sealed class Parent(
@Transient val foo: Long = 0L
)
@Serializable
data class Child(
val anything: Unit = Unit
): Parent(System.currentTimeMillis())
// main
Json.encodeToString<Parent>(Child()).apply {
println(this)
Json.decodeFromString<Parent>(this).apply {
println(this)
println(this.foo)
}
}
In my actual business, foo will be some dynamic values, each subclass is different, and even the type may be different, so I used @Transitient, and passed in the corresponding value when the subclass constructor called the parent class constructor, but after deserialization, the default value of the parent class was directly used.
{"type":"Child"}
Child(anything=kotlin.Unit)
0
Expected behavior
It should use value from subclass constructor
{"type":"Child"}
Child(anything=kotlin.Unit)
[Now timestamp]
Environment
- Kotlin version: 2.0.20-RC
- Library version: 1.6.0
- Kotlin platforms: JVM/Android
- Gradle version: 8.7
- IDE version (if bug is related to the IDE) IntelliJ IDEA 2024.2/Android Studio Koala 2024.1 Patch 2