Serialization nullables as @Optional by default?


#1

Hi,

I am just playing with kotlinx.serialization and am wondering if it is possible not to have to annotate fields as @Optional if they are nullable. For example this:

data class ItemWithOptionalValues(
    val id: Long,
    val name: String,
    val someOptionalField: String? = null,
    val anotherOptionalField: String? = null
)

would be nicer than this:

data class ItemWithOptionalValues(
    val id: Long,
    val name: String,
   
    @Optional
    val someOptionalField: String? = null,
    
    // Or even as a one liner
    @Optional val anotherOptionalField: String? = null
)

as the optional fields are clearly optional as they are nullable.

Is this achievable now or is it really hard to implement it in that way?

Regards


#2

#3

OK, thanks for that… I will follow it with interest :slight_smile: