I need help with this error `Sealed class cannot b...
# serialization
m
I need help with this error
Sealed class cannot be serialized as base class because it has property name that conflicts with JSON class discriminator 'type'. You can either change class discriminator in JsonConfiguration, rename property with @SerialName annotation or fall back to array polymorphism
Please note that changing the class discriminator causing the API to reject the payload.
Copy code
@Serializable
sealed class Message { abstract type: String }

@Serializable
sealed class MessageHeader { abstract type: String }

@Serializable
data class VideoHeader( override val type: String, val video: Media ): MessageHeader

@Serializable
data class TextMessage( override val type: String, text: String, header: MessageHeader ): Message
h
I'm not sure I understand what you want to do – you can't currently use a property as a discriminator (https://github.com/Kotlin/kotlinx.serialization/issues/1664). If you just want to have a
type
field in the JSON, you can remove the property and set the values for the classes via the
@SerialName
annotation. If you want both, you'll have to go custom.
m
Hi @hho I've created a sealed class named "`Message`" to handle serialization and deserialization of various message types required by the application. Within the "`Message`" object, there's a sealed class called "`Header`" which manages different message headers like image, video, document, and text. However, the deserialization process encounters an issue because both the parent and child classes share the same property name. If i'm not mistake the default JSON class discriminator is '`type`' and when I remove header everything works.