Polymorphism with kotlinx.serialization in Kotlin
Kotlin’s polymorphism allows objects of different classes to be treated through a common base type (sealed classes, interfaces, or abstract classes). When serializing to formats like JSON, we need a way to record and restore the actual subtype of each object. The kotlinx.serialization library provides tools to handle polymorphic JSON: a special class discriminator field in the JSON, annotations like @SerialName, and runtime registration of subclasses. By combining these, you can encode a common base class (or interface) and correctly decode the appropriate subtype.









