-
Notifications
You must be signed in to change notification settings - Fork 626
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
Option to not write optional values #58
Comments
So, did I understand you correctly? Given some class @Serializable
class Data(@Optional(writedefault=false) val value: Int = 2) you expect |
Yes that is correct. Maybe a better usecase is this one:
If optionalData is null you don't really want to have Another usecase is that you may want to read optional values but never write them (e.g. because they are deprecated) |
I wonder if that shall be a feature of the serializable class or a feature of the format? Does it make sense to automatically support elision of default values when both the class has eligible defaults (I'm thinking about compile-time constants and defaults here) and the format supports it? |
What do you mean with format? json, protobuffer, cbor? Think it depends on the use-case so IMHO it should be configurable. |
Btw, this is a very important feature for ProtoBuf that (unlike JSON) does not support the notion of Consider this case:
It crashes with
|
I am commenting here so I can easily find this issue again in the future, since I can't do that by subscribing alone. |
A major use-case for this is implementing the JSON-RPC spec. On the Request object, the spec says the Anyway, I know this is a feature under active development, just wanted to point out one more use case. :) |
My major question is what the intended semantics of optional are. Currently it means "may be missing in the input", but will always be written. It may be possible to just have a switch that specifies the semantics (eg. omit_if_null, omit_if_default_value, never_omit, omit_if_tag). That would really benefit from default value support in annotations. omit_if_tag would allow a tag to be set on the session (like that it is in compat mode) that can then be checked for individual fields without requiring custom serialization. Implementation is easy if this information is available in some form to the serializer. |
I have to vote for this, saves bandwidth when we serialize! |
Any news on this feature? |
New framework design contains solution for this problem: https://github.com/Kotlin/KEEP/blob/serialization/proposals/extensions/serialization.md#optionality |
@sandwwraith can you provide an exemple on how we can use the CompositeEncoder to not write optional value ? Can't find any docs on this :( |
@GoMino for now, workaround would be to write code like this: https://github.com/Kotlin/kotlinx.serialization/blob/eap13/runtime/common/src/test/kotlin/kotlinx/serialization/CustomSerializersTest.kt#L67 |
@sandwwraith thanks, is it compatible with kotlin Native and JS ? |
Yes, these are multiplatform interfaces |
Hello What if I need to skip. for example, |
@InsanusMokrassar Currently you are probably best of creating a custom encoder that does this in a way configured by that encoder (using annotations, a hardcoded list, initialisation of the encoder, etc.) |
@pdvrieze ok, thank you:) |
So there's a possible fix for this that I found which is kind of hacky but will resolve this for Kotlin data classes. Here's an example of a data class that I serialize which has an optional serializable parameter:
Currently with how the Kotlinx serialization library is written we write to our JSON map the value of So for the example above the resulting JSON object would look as follows:
Cool so this will mean I'm not going to change this value when I POST this payload right? NOPE
In the following file: Change this line:
to
Why? JSON treats the empty string, The goal of this is so our Data Class Serialized as JSON will look like this instead:
I'm not saying this is going to cover all use cases but for my purposes and the issues I've run into, I think this is a simple solution to a problem that's been causing me plenty of headaches. |
@davidbrazilparker so, you offer to change output of {"output": null} to {"output": ""} Is it right? I don't think that this solve better in case of skipping some values (like null) at least for the reason that it is not obviously enough. If to talk conversation topic: it is absolutely opposite to the main idea: skip fields serialization in some cases. |
@davidbrazilparker No, an empty string is not equivalent to a JSON null literal. It may be possible in your particular API, but not in general case. Even absence of value in some cases is not equivalent to a null literal. |
Yeah my mistake sorry guys 😛 |
In Jackson you can use |
@raderio can I use this with built-in JSON ( |
@InsanusMokrassar it is an example how this is done in Jackson, maybe will be good reference in implementation this here. |
@raderio Excuse me, I see |
Starting from You can create an instance of Json class with the corresponding setting: |
Is there a similar solution for configuring |
@sandwwraith I see this as a major ProtoBuf issue, because the protocol doesn't know NULL. And you give |
I'm sorry, but
As soon as I enable |
Is it possible to add an option to not write optional values?
For example, either if in nonstrict mode or add an parameter to
@Optional
. Something like:@Optional(dontwritewhen=2) val value: Int = 2
or
@Optional(writedefault=false) val value: Int = 2
The text was updated successfully, but these errors were encountered: