Hi, n00b Kotlin dev here: Is there a way to conver...
# serialization
b
Hi, n00b Kotlin dev here: Is there a way to convert the `val`s in the
@Serializable
data class while encoding? e.g. Given a data class: data class
MyClass(val accessible: Boolean)
and given the input JSON
{ "accessible": "yes" }
, can we convert the
accessible
to
Boolean
from
String
while encoding?
I tried the Primitive Serializer but my JSON has several other properties and I can’t seem to figure out how to write the
deserialize(decoder: Decoder)
function to convert the values to Bool
r
You can use
@Serializable(with = MySerializer::class)
on a property if that's what you mean. See https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#specifying-serializer-on-a-property
b
I solved it using
KSerializer
and overriding
deserialize()
I casted
decoder
to
jsonDecoder
and then got the keys and transformed manually.
@rnett Your solution is much more elegant, thanks!