is it possible to write a custom serializer (kotli...
# serialization
a
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
You could also write
error("This type cannot be deserialized.")
in the deserialization function.
r
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
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
super.deserialize()
because this is an
override
method. Really appreciate it!