-
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
Allow to always ignore nulls and empty collections when writing to JSON (regardless whether they are optional or not) #195
Comments
#58 , isn't it? |
This one has JSON-only scope and doesn't affect optionality/default values |
I can currently use the following for null values: val json = Json(encodeDefaults = false) That applies to all models though, no way to specify it on a per-property basis, and no support for empty collections. |
I guess...
... is not for the exact same use case. I'm currently searching for a solution where null values will not be part of the json, but I want to have other default values (like strings) be part of the json, so that they could be read from the consumer side. |
Is there a workaround for this? (apart from |
@FrancoSabadini Writing a custom serializer and applying it to the relevant properties. It's quite easy to do if you look at the doc, although it might be redundant if you have to apply it to many properties. |
Thanks for the answer @LouisCAD. Yes, I have to add it to quite a few properties so it gets a bit annoying. Can you point me to the specific doc you are referring to? And what would this custom serializer look like? |
@Serializable
data class Test(val value: String?)
fun main() {
val json = Json(
JsonConfiguration(encodeDefaults = false)
)
println(json.toJson(Test.serializer(), Test(null)))
} prints out {"value":null} |
That needs to be, @Serializable
data class Test(val value: String? = null) |
Thanks, what about when passing in null explicitly? |
If you only pass null explicitly, then it's not a default, so it is encoded. But by telling |
Yes, but title of the issue says I was just commenting on another flaw of this workaround. |
Any update on this? |
We do have a lot of default values. And they all are important. At the same time we do have some |
Any news on this? In the JSON world, |
I'm also in a situation where the encodeDefaults is not the same for me as encodeNulls. I have some defaults set to 0 (some Ints), where the server requires those 0's to be sent. So I need encodeDefaults = true, because I also use those Dto's for receiving data from the server, so I can't remove the default value on the constructor. But I would greatly appreciate getting rid of null-encodings, as it generates spam in the dto sent to the server and written in logs. |
We plan to work on it somewhere around 1.1 |
Any update on this? |
@carlosezam Unfortunately, this feature is not being developed in the moment |
I agree this feature should be added and should be completely separate from the "encodeDefaults = true" option. Consider this code: data class SomeDataClass(
val requiredField: String,
val timeout:Int = 30,
val someOptionalValue: String? = null,
val anotherOptionalValue: String? = null
)
val instance = SomeDataClass("some_value") By default this would output this json: {
"requiredField": "some_value"
} Which is not what we want, because it misses the "timeout" property. Keep in mind that the consumer of this API does not necessarily know the default value. The alternative would be to use encodeDefaults = true, which outputs this json: {
"requiredField": "some_value",
"timeout": "30",
"someOptionalValue": null,
"anotherOptionalValue": null
} Which is also not something we want, as this causes extra clutter, especially for data classes with a lot properties with null as a default. My proposal would be to add a new option (eg: encodeNulls), which is true by default for backwards compatability, but would remove all nulls if set to false. It would then output this json: {
"requiredField": "some_value",
"timeout": "30"
} We're currently converting our JVM Kotlin project to a Multiplatform one. We used gson before, where this was a possibility, but we found no way to do this using kotlinx.serialization. |
Any news on this? |
Providing EncodeDefault annotation per field can be helpful to this situation. |
No description provided.
The text was updated successfully, but these errors were encountered: