I'm using kotlinx-serialization-json
I have this class:
@Serializable
data class Event(
@SerialName("temperature") val temperature: Float?,
@SerialName("pressure") val pressure: Float?,
@SerialName("humidity") val humidity: Float?,
)
and this call
Json.encodeToString(Event(temperature = 42.0f, pressure = null, humidity = 20.9f))
During serialization I receive such json:
{
"temperature": 20.5,
"pressure": null,
"humidity": 20.9
}
but I would like to prevent serializing null values and receive this:
{
"temperature": 20.5,
"humidity": 20.9
}
It's a problem for me, because during serializing lengthy list of events I waste a lot of lines. Anyone's got an idea how to achieve this?
EDIT:
There is new simple way to achieve this: https://blog.jetbrains.com/kotlin/2021/09/kotlinx-serialization-1-3-released/#excluding-nulls