I am using Kotlinx serializer for serializing/des...
# android
k
I am using Kotlinx serializer for serializing/deserializing of data classes with retrofit. I need to skip the null values from serialization, but I couldn’t find the way to do that here is my data class
Copy code
@Serializable
data class Attributes(
    val home: String? = null,
    val room: String? = null,
    val announcement: Int? = null,
    val push_notification_enabled: Int? = null
)
when I called Attributes().serialize() , the resulting json as below
Copy code
"attributes":{
      "home":null,
      "room":"null",
      "announcement":null,
      "push_notification_enabled":null
   }
what is expected is
Copy code
"attributes":{}
Note: serialize() is an extension function as below
Copy code
inline fun <reified T: Any> T.serialize(): String {
    return Json.stringify(T::class.serializer(), this)
}
Basically I want to exclude all null params from serialization. I tried with by setting
JsonConfiguration( encodeDefaults = false)
, but still resulting json includes
null
fields from data class. Anyone has idea of how to do that
o
#serialization
j
you want JsonTransformingSerializer to drop the nulls after you serialize. https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json_transformations.md