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

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:

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


Link copied to clipboard


Link copied to clipboard