Skip to content

Serialization issue with value classes and class discriminatorΒ #3022

@ArmureSolide

Description

@ArmureSolide

Describe the bug
When trying to serialize a value class
If the ClassDiscriminatorMode is set to ALL_JSON_OBJECTS
And the value class encapsulates a list
Then it seems like the serializer tries to add a classDiscriminator to a list, resulting in a weird output.

This issue doesn't seem to exist if the value class encapsulates an object or a primitive

To Reproduce

@Serializable
@JvmInline
value class Test(val value: List<Int>)

@OptIn(ExperimentalSerializationApi::class)
val json: Json by lazy {
    Json {
        classDiscriminatorMode = ClassDiscriminatorMode.ALL_JSON_OBJECTS
    }
}

fun main() {
    val test = Test(value = listOf(3))
    val result = json.encodeToString(test)
    println(result) // ["type":"Test",3]
}

Expected behavior

The output should be [3]

Environment

  • Kotlin version: 2.1.21
  • Library version: 1.8.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions