Hey all! I've got an event stream from JS (legacy...
# serialization
e
Hey all! I've got an event stream from JS (legacy library) into Kotlin. I've got data classes under sealed classes that I want to map from type names to serializers so that I can easily parse from an event to the correct object. Here are the classes:
Copy code
sealed class ManyHandsData: JSAction() {
        @Serializable
        data class handError(val message: String, val errorCode: Int): ManyHandsData()

        @Serializable
        data class DATA_INPUT(val openSensorInput: Int, val closeSensorInput: Int): ManyHandsData()

        @Serializable
        data class currentGripDidChangeExtended(val currentGrip: Int, val buttonSelected: Int, val currentButtonIndex: Int): ManyHandsData()
    }
I've tried this:
Copy code
internal val handDataActionSerializers = listOf(
    JSAction.ManyHandsData.handError.serializer(),
    JSAction.ManyHandsData.DATA_INPUT.serializer(),
    JSAction.ManyHandsData.currentGripDidChangeExtended.serializer()
).associateBy { it.descriptor.name}
which doesn't have the simple name. I was thinking maybe Delegates are the way to go (doing this for mapping js methods) but I'm struggling to think how to integrate that with data classes.. The goal would a mapping similar to above which allows me to retrieve a serializer by the string of an identifier (eg "DATA_INPUT")
Simplest solution is probably to just parse out the simpleName from the descriptor name, that feels kinda dirty though.
Followed my own advice:
Copy code
internal val handDataActionSerializers = listOf(
    JSAction.ManyHandsData.handError.serializer(),
    JSAction.ManyHandsData.DATA_INPUT.serializer(),
    JSAction.ManyHandsData.currentGripDidChangeExtended.serializer()
).associateBy { it.descriptor.name.split(".").last() }
Let me know if you come up with a better solution. Would be great if I didn't have to list them out.