https://kotlinlang.org logo
#serialization
Title
# serialization
d

dave08

02/26/2023, 4:26 PM
Is it true that Kotlinx Serialization leaves a value out of json if it == it's default value?
Like:
Copy code
data class Foo(val name: String, val bl: Boolean = true)
val foo = Foo("bar", true)
// Becomes:
// { "name": "bar" } not { "name": "bar", "bl": true }?
a

Adam S

02/26/2023, 5:02 PM
yes
Default values are not encoded by default in JSON. This behavior is motivated by the fact that in most real-life scenarios such configuration reduces visual clutter, and saves the amount of data being serialized.
https://github.com/Kotlin/kotlinx.serialization/blob/v1.5.0/docs/basic-serialization.md#defaults-are-not-encoded-by-default
d

dave08

02/26/2023, 8:16 PM
A bit unexpected though... Especially since on client side, the default was the opposite...
9 Views