kotlinkotlinx.serializationkotlinx

kotlinx de/serialization sealed class/enum


Writing chat app using kotlin, historically from nodejs I have websocket json objects in/output format:

{"action": "join:chatroom", "payload", "chatroom-name"}
{"action": "leave:chatroom", "payload", "chatroom-name"}
{"action": "message", "payload", {"id": 1, "text": "blah", "user": {"name": "Roman"}}}
{"action": "notification", "payload", {"id": 1, "verb": "blah", "user": {"name": "Roman"}}}

trying to adopt kotlinx serialization to be able to work with this type of approach with no luck seems like sealed class is the closest what I can try but how to change type to action

took this approach https://ahsensaeed.com/sealed-classes-kotlinx-serialization-deserialization/ trying to do something like:

@Polymorphic
@Serializable
sealed class BackendAction{
    @Serializable
    data class Auth(
//        val action: String = "authorize",
        val payload: String = ""
    ) : BackendAction()
}

class BackendActionSerializer {
    private val sealedModule = SerializersModule {
        polymorphic(BackendAction::class) {
            BackendAction.Auth::class with BackendAction.Auth.serializer()
        }
    }

    private val serializer: KSerializer<BackendAction>
        get() = BackendAction.serializer()

    val format: StringFormat
        get() = Json(configuration = JsonConfiguration(classDiscriminator = "action"), context = sealedModule)

    fun toStringT(json: String): BackendAction {
        return format.parse(serializer, json)
    }

    fun toTString(t: BackendAction): String {
        return format.stringify(serializer, t)
    }
}

and call:

    val act = BackendAction.Auth("blah")
    val serializer = BackendActionSerializer()
    val data = serializer.toTString(act)

gives {"action":"com.project.path.BackendAction.Auth","payload":"blah"} so I need mapping com.project.path.BackendAction.Auth to authorize

EDIT:

answering my question: @SerialName("authorize") it was easy :)

@Polymorphic
@Serializable
sealed class BackendAction{
    @Serializable
    @SerialName("authorize")
    data class Auth(
        var payload: String = ""
    ) : BackendAction()
}

working web ui for this chat app here: https://www.lonje.com/


Solution

  • @SerialName("authorize") do the trick

    @Polymorphic
    @Serializable
    sealed class BackendAction{
        @Serializable
        @SerialName("authorize")
        data class Auth(
            var payload: String = ""
        ) : BackendAction()
    }