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

Support array content equality when deciding whether to encode a default value

Open drewhamilton opened this issue 2 years ago • 7 comments

What is your use-case and why do you need this feature?

For most properties, the behavior of encodeDefaults or @EncodeDefault is determined by an equals check against the property's default value: If encodeDefaults is false and equals is true, the property doesn't get included when serialized.

For array properties, this doesn't work well, because arrays don't implement content-based equals. Thus, for example, an empty array property value registers as not-equals to the empty array default property value, and the empty array is included in the serialized output.

cashapp/redwood#1395 wants to migrate its public API List properties to Array types, primarily because of performance impact on JS.

Describe the solution you'd like

Support content-based rather than reference-based comparison when determining the encodeDefaults behavior for array properties. This could be by default or via a property annotation.

drewhamilton avatar Aug 09 '23 21:08 drewhamilton

Given that .equals on arrays aren't as useful, I think the behavior can be changed. Although, it has to be a bugfix in compiler plugin, so it will take some time until it's available in the Kotlin release

sandwwraith avatar Aug 10 '23 12:08 sandwwraith

Any thoughts on when this could be implemented? Any chance of landing around Kotlin 2.0 release?

drewhamilton avatar Dec 06 '23 18:12 drewhamilton

Tbh I forgot about this, so it does not have any plans or particular date

sandwwraith avatar Dec 06 '23 18:12 sandwwraith

Now that Kotlin 2.0 is out do you think this is something that could be put into future consideration?

JakeWharton avatar May 29 '24 18:05 JakeWharton

Yes, I think it is something that can be done now

sandwwraith avatar May 30 '24 10:05 sandwwraith