Hello Anybody knows how to exclude null values whe...
# announcements
o
Hello Anybody knows how to exclude null values when we try to serialize some object into, for example, json?
g
Depends on your serialization library
o
JSON
g
it is not a library, this is a format
o
I am using default JSON library. How can I find it exact?
g
default for what?
o
for serialization/deserialization
h
If you're using the kotlinx.serialization library, the answer is no, you cannot exclude null values from serialization.
Cf. my question less than a week back in #C7A1U5PTM
g
What is “default JSON library”, default for which platform? There is no default JSON library for Kotlin and no default JSON for Java/JVM
h
With Jackson, it's different. If you're using Jackson for serialization, mark the stuff that shouldn't be serialized if null, with some annotation. Forgot the wording of it right now, but ask uncle Google or something.
c
@JsonInclude(Include.NON_NULL)
g
Before asking Google make sense to understand which API/library you are using 🤷
1️⃣ 1
o
I already have found this answer, but this answer touch some exact library when I have tried to solve this problem by more common way
thank you
h
More common? I believe Jackson is pretty common. I wouldn't worry about that. But you need to be specific when you're asking, for as you see, the possible responses are many ...
g
@Ovsyannikov Alexey What is “more common way”? what kind library are you talking about?
o
I am using
JSON
for stringify objects. If to be honest, I don't know what
kotlin.serializations
now use by default in
kotlinx.serialization.json.JSON
g
so okay,
kotlinx.serialization
h
Then see my first answer. The reason your question is confusing us, is that `JSON`is not a library. It's just a format.
g
This is not supported by kotlinx.serialization for now https://github.com/Kotlin/kotlinx.serialization/issues/195
o
Ok, thank you
And I have read this too, but hoped that available some other way for this feature
h
Yeah... asking is just fine. I think maybe you should have told us from the beginning what you already have tried, that would have made it easier for us to reply as expected more quickly ...
g
As I understand the only workaround for now: write own serializer or copy paste kotlinx.serialization.json.JSON implementation and change it to skip null values, I didn’t try but probably this is possible
o
I see
g
And in general, unfortunately for now`kotlin.serializations` is not “default” and not even “common way”. It’s experimental library and compiler plugin under active development and last time mostly focused on API standardisation of APIs rather than on particular format features
154 Views