EncodeDefault

annotation class EncodeDefault(val mode: EncodeDefault.Mode = Mode.ALWAYS)

Controls whether the target property is serialized when its value is equal to a default value, regardless of the format settings. Does not affect decoding and deserialization process.

Example of usage:

@Serializable
data class Foo(
@EncodeDefault(ALWAYS) val a: Int = 42,
@EncodeDefault(NEVER) val b: Int = 43,
val c: Int = 44
)

Json { encodeDefaults = false }.encodeToString((Foo()) // {"a": 42}
Json { encodeDefaults = true }.encodeToString((Foo()) // {"a": 42, "c":44}

See also

Constructors

Link copied to clipboard
fun EncodeDefault(mode: EncodeDefault.Mode = Mode.ALWAYS)

Types

Link copied to clipboard
enum Mode : Enum<EncodeDefault.Mode>

Strategy for the EncodeDefault annotation.

Properties

Link copied to clipboard
val mode: EncodeDefault.Mode