kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Deserialize null/missing fields to empty collections with default values

Open jpilch opened this issue 3 weeks ago • 0 comments

What is your use-case and why do you need this feature? I would like to deserialize JSON where a collection field can be null, missing, or an empty array, and have all three cases result in an empty collection in the deserialized object.

Describe the solution you'd like With the following data class:

@Serializable
data class MyDto(
  val name: String,
  val items: List<String>
)

I'd like for all three JSON inputs to successfully deserialize with items as an empty list:

val json = Json {}

// All three should work:
json.decodeFromString<MyDto>("""{"name":"test","items":[]}""")
json.decodeFromString<MyDto>("""{"name":"test","items":null}""")
json.decodeFromString<MyDto>("""{"name":"test"}""")

jpilch avatar Nov 26 '25 14:11 jpilch