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
Dominaezzz
11/17/2021, 8:07 PM
You could just use
JsonPrimitive
?
Dominaezzz
11/17/2021, 8:09 PM
Actually I think you should. It's a type the serializer will understand and it does what you describe.
j
jean
11/18/2021, 6:50 AM
I didn’t see this one before, thanks for the help 🙂