annotation class JsonClassDiscriminator(val discriminator: String)

Specifies key for class discriminator value used during polymorphic serialization in Json. Provided key is used only for an annotated class and its subclasses; to configure global class discriminator, use JsonBuilder.classDiscriminator property.

This annotation is inheritable, so it should be sufficient to place it on a base class of hierarchy. It is not possible to define different class discriminators for different parts of class hierarchy. Pay attention to the fact that class discriminator, same as polymorphic serializer's base class, is determined statically.


abstract class Base

@Serializable // Class discriminator is inherited from Base
abstract class ErrorClass: Base()

class Message(val message: Base, val error: ErrorClass?)

val message = Json.decodeFromString<Message>("""{"message": {"message_type":"my.app.BaseMessage", "message": "not found"}, "error": {"message_type":"my.app.GenericError", "error_code": 404}}""")

See also


Link copied to clipboard
fun JsonClassDiscriminator(discriminator: String)


Link copied to clipboard
val discriminator: String