I’m working with a backend returning a `value` att...
# serialization
j
I’m working with a backend returning a
value
attribute as part of a json payload. That attribute can potentially be of any primitive type or null. I started with a custom serializer :
Copy code
class NullableAnySerializer : KSerializer<Any?> {
    override val descriptor: SerialDescriptor = // What should it be here?

    override fun serialize(encoder: Encoder, value: Any?) {
        when (value) {
            null -> encoder.encodeNull()
            is Float -> encoder.encodeFloat(value)
            is Boolean -> encoder.encodeBoolean(value)
            else -> throw IllegalStateException("unsupported type")
        }
    }

    override fun deserialize(decoder: Decoder): Any? {
        // What should it be here?
    }
}
I’m stuck at the level of
descriptor
and
deserialize()
what should I use in those places?
d
You could just use
JsonPrimitive
?
Actually I think you should. It's a type the serializer will understand and it does what you describe.
j
I didn’t see this one before, thanks for the help 🙂