Kotlin / kotlinx.serialization

Kotlin multiplatform / multi-format serialization
Apache License 2.0
5.34k stars 618 forks source link

External generated sealed class serializer returns empty json. #970

Open Dominaezzz opened 4 years ago

Dominaezzz commented 4 years ago

Describe the bug Serializer returns {} when deserializing

To Reproduce Attach a code snippet or test data if possible.

    @Serializable(AnimalSerializer::class)
    sealed class Animal {
        @Serializable
        data class Dog(val breed: String) : Animal()
        @Serializable
        data class Cat(val color: String) : Animal()
    }

    @Serializer(forClass = Animal::class)
    object AnimalSerializer

    @Test
    fun testReproduce() {
        println(Json.encodeToString(Animal.serializer(), Animal.Dog("German Shephard")))
    }

Expected behavior

{"type":"io.github.matrixkt.EventTypeTests.Animal.Dog","breed":"German Shephard"} instead of {}

Environment

ganfra commented 3 years ago

@sandwwraith any news about it?

sandwwraith commented 3 years ago

External serialization is now considered an experimental feature and it's not a top priority, so I can't tell any particular timeframe for the fix

Dominaezzz commented 3 years ago

Rip, that's unfortunate. I was using this as a workaround for https://github.com/Kotlin/kotlinx.serialization/issues/546, by using a JsonTransformingSerializer to rewrite the class discriminator. I guess I'll use a PolymorphicSerializer for now.