I am trying to figure out polymorphism in kotlinx ...
# announcements
b
I am trying to figure out polymorphism in kotlinx serialization with no success. Here's the code I am trying to run
Copy code
@Serializable
@Polymorphic
data class JsonMessage(
    @Polymorphic val command: JsonCommand
)

@Serializable
@Polymorphic
open class JsonCommand

@Serializable
@Polymorphic
data class NewL2SnapCommand(
    val symbol: String
) : JsonCommand()

@UnstableDefault
fun main() {
    val message = JsonMessage(
        NewL2SnapCommand(
            symbol = "ABC"
        )
    )

    val snapJson = Json.stringify(JsonMessage.serializer(), message)
    println(snapJson)
}
This results in
Exception in thread "main" kotlinx.serialization.SerializationException: class jsonApi.NewL2SnapCommand is not registered for polymorphic serialization in the scope of class jsonApi.JsonCommand
The documentation for this is kinda sparse since it's a new feature, so any help would be appreciated.