You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug SealedClassSerializer doesn't serialize value subclasses into the expected structure, with type property. Instead it just dumps the raw value, which won't deserialize back!
To Reproduce
See test below
Expected behavior
Instead of raw value, the resulting JSON should be of the same structure, as regular subclasses: "{"type":".......","value":......}"
Environment
Kotlin version: 2.0.21
Serialization: 1.7.3
Gradle 8.8
Android Studio Ladybug
import kotlin.test.assertEquals
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import org.junit.Test
@Serializable
sealed interface Sealed {
@Serializable
@SerialName("DataObject")
data object DataObject : Sealed
@Serializable
@SerialName("DataClass")
data class DataClass(val code: Int) : Sealed
@Serializable
@SerialName("ValueClass")
@JvmInline
value class ValueClass(val code: Int) : Sealed
}
class SealedTest {
@Test
fun test() {
val serializer = Sealed.serializer()
assertEquals(
expected = "{\"type\":\"DataObject\"}",
actual = Json.encodeToString(serializer, Sealed.DataObject),
)
assertEquals(
expected = "{\"type\":\"DataClass\",\"code\":111}",
actual = Json.encodeToString(serializer, Sealed.DataClass(111)),
)
assertEquals(
expected = "222", // WRONG! should be "{\"type\":\"DataClass\",\"value\":222}"
actual = Json.encodeToString(serializer, Sealed.ValueClass(222)),
)
}
}
The text was updated successfully, but these errors were encountered:
This is caused by the fact that your ValueClass is an inline class over Int. There is simply nowhere to add type, because Int is not an object. See #2049 (comment)
Describe the bug
SealedClassSerializer
doesn't serialize value subclasses into the expected structure, withtype
property. Instead it just dumps the raw value, which won't deserialize back!To Reproduce
See test below
Expected behavior
Instead of raw value, the resulting JSON should be of the same structure, as regular subclasses: "{"type":".......","value":......}"
Environment
The text was updated successfully, but these errors were encountered: