Currently, encodeDefaults = true writes all default values, while encodeDefaults = false omits all of them.
There is no way to include only non-null defaults.
Motivation
In many API contexts, it is desirable to serialize default values that carry meaningful information but skip those that are null by default.
This keeps payloads compact and avoids sending meaningless null entries while still preserving useful defaults.
@Serializable
data class UserProfile(
val id: String,
val nickname: String?,
val displayName: String? = null,
val country: String = "DE",
val avatarUrl: String? = null,
)
Rationale:
This would allow developers to keep meaningful defaults (like "DE") while automatically skipping null defaults, avoiding the need for custom serializers or post-processing.