Skip to content

Add option to encode only non-null default values in encodeDefaults #3096

@jschneider

Description

@jschneider

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions