I'm deserializing a `Date?` from a `String?` using...
# serialization
r
I'm deserializing a
Date?
from a
String?
using a custom
KSerializer<Date>
and it's working great, until the 3rd party API I'm using sends an empty string instead of null as expected. Is there a way to map this empty case to a null Date instead of a dummy-Date? It seems something like
KSerializer<Date?>
is not supported, is that correct?
f
To get a nullable serializer of something do .nullable like .list
(Actually I’m not sure if this was pushed yet, if not you can wrap your date serializer into a NullableSerializer instead)
r
Well the problem isn't just nullability, nulls map to each other fine. But during deserialization I want to map an empty string to a null date, but
KSerializer<Date>
does not allow null return types. (And
KSerializer<Date?>
does not match the
Date?
types on my class for some reason) How can I use
.nullable
in an annotation? I assumed this was used under the hood already for me since my Date types are nullable. At the end, I would like a deserializer that does the following:
String?
->
Date?
where
null
or
""
maps to
null: Date?
k
if you want to decode a nullable element the serializer calls the
decodeNullableElement
of the compositedecoder which will most likely call
decodeNotNullMark()
on the decoder which will return false if the next value is null.
So unless you have custom decoder I don't see how you'd fix it.
r
Yeah that's what I figured, thanks! I have a workaround right now and just pick a reasonable default.