Hi folks, using Kotlinx.serialization with Ktor f...
# serialization
k
Hi folks, using Kotlinx.serialization with Ktor for JSON content
Copy code
json(Json {
    prettyPrint = true
    isLenient = true
    encodeDefaults = true
    ignoreUnknownKeys = true
    useAlternativeNames = true
    coerceInputValues = true
    explicitNulls = false
})
this is my config despite having
encodeDefaults = true
I am unable to encode default values of data class to Json Am I doing something wrong here or is this an existing issue?
g
explicitNulls = false ?
k
yeah… that way I don’t encode
nulls
and when keys are missing while decoding default values are set.
g
Feels like "I want to encode defaults" and "I don't want to encode nulls" are conflicting in my mind (ex:
val foo: String? = null
=> encode or not ??). Maybe one settings is changing the other.
🤔 1
k
I have done this as per the documentation and
encodeDefaults = true
&
explicitNulls = false
by the naming convention it should do the expected but it doesn’t
n
is there any default values that are not null ?
k
yes there are
n
this seems to be behave the way i expect it to
Copy code
@Serializable
data class A(
	val a: String? = null,
    val b: Int = 0,
)
serializing this data class with default values ... and the settings you are using
a
gets skipped because it ends up being
null
and
b
gets serialized https://pl.kotl.in/-YkdiyRId
k
thanks. I realised the mistake I was making.