https://kotlinlang.org logo
#ktor
Title
# ktor
b

bmarinovic

03/30/2020, 8:42 PM
Hi, is it possible to enforce required/optional fields in JSON serialization? It seems that kotlinx.serialization does the job of enforcing that fields should be required if no defaults are provided, but I was wondering if I can make some optional by saying it’s nullable with e.g.
String?
or
Optional<String>
?
s

SerVB

03/30/2020, 8:45 PM
AFAIR,
kotlinx.serialization
can use default values from the constructor. Have you checked?
b

bmarinovic

03/30/2020, 8:49 PM
yes, but that’s not really a same thing. I would have to check for each value is it default. And it would become bloated with complex classes
s

SerVB

03/30/2020, 9:09 PM
What do you want to do then? Aren't you requesting for something like
@Serializable class MyData(val optionalString: String? = null)
?
6 Views