is it possible to write a custom serializer (kotlinx.serialization) for serializing only, and not overriding deserialize? I get an error if I don't specifically override deserialize too. Alternatively, how can I write the deserialize method to use the default or built-in deserializer? (this is for a data class, not a primitive). There's no
super
to leverage. Thank you.
m
Marc Knaup
10/22/2020, 11:09 AM
You could also write
error("This type cannot be deserialized.")
in the deserialization function.
r
russhwolf
10/22/2020, 1:05 PM
I guess you could implement
SerializationStrategy
instead of
KSerializer
if you just want to go one-way. Depending on which APIs you interact with that might be limiting, but I think they've tried to make stuff flexible enough to support that use-case.
For falling back to the built-in, have you tried delegating to
YourClass.serializer().deserialize(...)
?
✔️ 2
a
andyg
10/22/2020, 10:33 PM
thanks @russhwolf - both your suggestions helped. Yes a new
SerializationStrategy
was ultimately needed, because I'm serializing a class not a primitive. Also, yes
YourClass.serializer().deserialize(...)
appears to work, I had been expecting something like