-
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
Provide @AlwaysEncode / @EncodeDefault annotation #1091
Comments
For null-not-supported formats like ProtoBuf, it is needed to encode nullable value with |
Describe the solution you'd likeAdd a enum and a annotation public enum EncodeDefaultMode{
DEFAULT,
ALWAYS,
NEVER
}
@Target(AnnotationTarget.PROPERTY)
public annotation class EncodeDefault(public val mode: EncodeDefaultMode = EncodeDefaultMode.DEFAULT) Then:
|
Can you, please, explain, why there is a need in this enum values (especially NEVER)? Original issue was just about Also, default value of annotation parameter (DEFAULT) seems dull: when |
If you think so,
To provide a more precise control of optionality (per field), of course.
Well, |
One usage case about |
In this PR, It can be also designed as |
I'm in favour of this feature request, but I'd make 2 annotations for simplicity, I agree with @sandwwraith that the
But that's unrelated to this feature request, I think. |
It is indeed simplified. But enum will indicate that So we may have to investigate further to make a choice. |
The existing annotation The doc for
which sounds a bit misleading, because it affects encoding (not only decoding). |
However, |
@ArcticLampyrid sorry, could you explain again? 😕 What I see is that with
Isn't this what the proposed |
While decoding,
|
@ArcticLampyrid aaah. makes sense now, thanks for the explanation 👍 |
Control
|
For further: |
Thanks for the study; I'd still favor the argument-less Regarding Protobuf, null handling is a completely different question, that is under implementation now |
I wonder how this is supposed to interact with formats that mandate that default values are not encoded. DER (a strict subset of BER) is an example of such format (which I happen to be implementing). It's important for signing to make sure all DER encoders generate the exact same DER encoded object so signatures are consistent. |
@Dominaezzz |
BTW, in your example of DER format, it seems that DER encoder support all modes ( It' similar that we can omit all defaults in JSON while the client assume that we never omit any defaults, so we use |
Wrap optional input arguments in new `OptionalInput` sealed class which can either be `Undefined` (omitted) or `Defined` (value or explicit null). Unfortunately it looks like we won't be able to support this functionality for `kotlinx-serialization` until Kotlin/kotlinx.serialization#1091 is resolved. Related: ExpediaGroup#1151
* [client] support optional input for jackson serializer Wrap optional input arguments in new `OptionalInput` sealed class which can either be `Undefined` (omitted) or `Defined` (value or explicit null). Unfortunately it looks like we won't be able to support this functionality for `kotlinx-serialization` until Kotlin/kotlinx.serialization#1091 is resolved. Related: #1151 * fix build * add explicit opt-in flag for plugins to use optional input wrapper
…1158) * [client] support optional input for jackson serializer Wrap optional input arguments in new `OptionalInput` sealed class which can either be `Undefined` (omitted) or `Defined` (value or explicit null). Unfortunately it looks like we won't be able to support this functionality for `kotlinx-serialization` until Kotlin/kotlinx.serialization#1091 is resolved. Related: ExpediaGroup#1151 * fix build * add explicit opt-in flag for plugins to use optional input wrapper
What is your use-case and why do you need this feature?
kotlinx.serialization framework allows to drop properties from output when its values are equal to default ones (see
encodeDefault
flag). However, this is configurable per format and not per property. Sometimes, it may be convenient to have a default value in Kotlin class, but it also may be necessary to always serialize this value to JSON — for example, if this is a protocol/api version for some app.See also: #58 (comment)
Describe the solution you'd like
Annotation with property target that allows to force encoding even if property value is equal to the default one.
The text was updated successfully, but these errors were encountered: