Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserializing, Transitient field use default value ​​instead of the values ​​from subclass constructor #2785

Open
Gargantua7 opened this issue Aug 16, 2024 · 0 comments
Labels

Comments

@Gargantua7
Copy link

Gargantua7 commented Aug 16, 2024

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
@Gargantua7 Gargantua7 changed the title Deserializing, Transit field use default value ​​instead of the values ​​from subclass constructor Deserializing, Transitient field use default value ​​instead of the values ​​from subclass constructor Aug 16, 2024
@sandwwraith sandwwraith added the bug label Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants