You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(!) I am aware of the encoding of defaults and understand that they are not encoded by default: Encoding Defaults Documentation.
Please close this issue as a duplicate with a reference to the original one, if it exists.
Consider the following scenario where I have a default value for my field b:
@Serializable
data classA(
vala:String?,
valb:String = "b"
)
If I pass a different value explicitly:
val value =A("a", "b1")
Json.encodeToString(value)
The encoded output is as expected:
{ "a": "a", "b": "b1" }
However, if I pass the defaultvalueexplicitly:
val value =A("a", "b")
Json.encodeToString(value)
The encoded output is not what you might expect:
{ "a": "a" }
My team encountered a production error because they did not check if the default compile-time value was equal to the non-default runtime value, and they expected this field to be serialized.
The motivation behind this behavior is clear: it is challenging to distinguish between the default value and an explicitly set value at the frontend level. However, it would be beneficial to mention this explicitly in the README.
The text was updated successfully, but these errors were encountered:
There's no way to distinguish between these two "b"s. In the same way, you can't distinguish between these calls in Kotlin:
funfoo(b:String = "b") {
// you can't tell if "b" is a default value or not
}
foo()
foo("b") // identical call
Actually, the very purpose of default values is to be indistinguishable from user-passed ones. Consider using nulls or wrappers if you need a special differentiation.
(!) I am aware of the encoding of defaults and understand that they are not encoded by default: Encoding Defaults Documentation.
Please close this issue as a duplicate with a reference to the original one, if it exists.
Consider the following scenario where I have a default value for my field
b
:If I pass a different value explicitly:
The encoded output is as expected:
However, if I pass the default value explicitly:
The encoded output is not what you might expect:
My team encountered a production error because they did not check if the default compile-time value was equal to the non-default runtime value, and they expected this field to be serialized.
The motivation behind this behavior is clear: it is challenging to distinguish between the default value and an explicitly set value at the frontend level. However, it would be beneficial to mention this explicitly in the README.
The text was updated successfully, but these errors were encountered: