Hello With Kotlin Serialization, I need to adapt a...
# serialization
d
Hello With Kotlin Serialization, I need to adapt a decode value. An API can return: • Valid value • Or empty string I would like to set my variable as
MyExpectedType?
(nullable) and the value must be null when the String is empty. Without modification into the custom serializer, is it possible to have this behavior ?
a
Are you dealing with JSON? Is the valid value also a string, or is it an object? (Some example data would help, can you share some?)
d
The valid value is a String parsed as Duration
• Invalid value: null / "" • Valid value: "1m", / "10d13d"
a
If you don't want to deal with a custom serializer then one easy way is to decode it as a string and define an additional property in the class, e.g.
val duration: Duration get() = ...
, and only convert it to a duration if the string isn't blank