Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Quantum64 opened this issue Mar 17, 2023 · 1 comment

Comments

@Quantum64
Copy link

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

@k163377
Copy link
Contributor

k163377 commented Mar 17, 2023

The concrete default values exist only on the bytecode and cannot be read or used for comparison.
Thus, it is theoretically impossible to implement this correctly.

It is also difficult to include a default value in a property if it is specified in the factory.
This is because it appears to be difficult to get the creator in this processing scope.

https://www.javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind/latest/com/fasterxml/jackson/databind/AnnotationIntrospector.html#findPropertyInclusion-com.fasterxml.jackson.databind.introspect.Annotated-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants