Skip to content

Kogera should consider data class property defaults with JsonInclude.Include.NON_DEFAULT #84

@Quantum64

Description

@Quantum64

This is a shortcoming in jackson-module-kotlin that I hoped could be fixed by this project.

Consider the mapper

val mapper = ObjectMapper()
        .registerKotlinModule()
        .setSerializationInclusion(JsonInclude.Include.NON_DEFAULT)

and the class

data class TestData(
    val defaulted: Boolean = true
)

When we serialize TestData where defaulted = false with this mapper, the field is skipped since the mapper assumes that false is the default value for a boolean property. However, when the value is deserialized, the default value for the property is used, and this causes a roundtrip failure.

val data = TestData(defaulted = false)
println(data) // TestData(defaulted=false)
println(println(mapper.writeValueAsString(data))) // {}
println(mapper.readValue(mapper.writeValueAsString(data), TestData::class.java)) // TestData(defaulted=true)

Is there any way this new approach with kotlinx-metadata can be used to determine when a property's value matches the default constructor value and adjust serialization appropriately, or maybe just always include properties when the default is non-standard?

Here is the corresponding issue for jackson-module-kotlin: FasterXML/jackson-module-kotlin#478

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions