-
Notifications
You must be signed in to change notification settings - Fork 619
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
encodeDefaults = false
doesn't work for fields of interface type in some cases
#1564
Comments
|
Thanks for the clarification of internal implementation, but this behavior still doesn't make sense to me. If serialized forms are the same, then the absent value would be reconstructed during decoding anyway. So the goal of "encodeDefaults = false" flag ("reduces visual clutter, and saves the amount of data being serialized") is not reached. Users have to either override Probably fair serialization of actual and default property (for subsequent comparing) is too expensive for default behavior. So I'd suggest either add some new configuration option on format level (so that 3 strategies regarding encoding defaults could be set: desired and two current), or (better) add a new method for public interface SerializationStrategy<in T> {
//...
//Will be called to compare default value with actual; takes effect only for properties serialization;
// default value will not be encoded if encodeDefaults=false and this method returned true
fun checkEqualityToDefault(default: T, actual: T): Boolean {
return default == actual
}
} So, it could be overridden in custom serializer for property ( object IntInterfaceWrapperSerializer : KSerializer<IntInterfaceWrapper> {
//...
override fun checkEqualityToDefault(default: IntInterfaceWrapper, actual: IntInterfaceWrapper): Boolean {
return default.x == actual.x
}
} Also this partly restores feature parity for custom serializer with plugin-generated (regarding new annotations suggested in #1091):
But if both features would be implemented, they may clash - so there need to be either some priority rules between |
I'm not sure if all this complex mechanism is better than simple |
Describe the bug
encodeDefaults = false
doesn't have an effect for fields of interface type if:To Reproduce
Consider the following code snippet. It defines and serializes the
IntBox
class, all fields of which have default values:Expected behavior
Console output should be:
{}
, but it's{"wrapperImpl3":0,"wrapperImpl1ResetToImpl2":0,"wrapperImpl2ResetToImpl1":0}
Environment
The text was updated successfully, but these errors were encountered: