coerceInputValues

Enables coercing incorrect JSON values in the following cases:

  1. JSON value is null but the property type is non-nullable.

  2. Property type is an enum type, but JSON value contains an unknown enum member.

Coerced values are treated as missing; they are replaced either with a default property value if it exists, or with a null if explicitNulls flag is set to false and a property is nullable (for enums).

Example of usage:

enum class Choice { A, B, C }

@Serializable
data class Example1(val a: String = "default", b: Choice = Choice.A, c: Choice? = null)

val coercingJson = Json { coerceInputValues = true }
// Decodes Example1("default", Choice.A, null) instance
coercingJson.decodeFromString<Example1>("""{"a": null, "b": "unknown", "c": "unknown"}""")

@Serializable
data class Example2(val c: Choice?)

val coercingImplicitJson = Json(coercingJson) { explicitNulls = false }
// Decodes Example2(null) instance.
coercingImplicitJson.decodeFromString<Example1>("""{"c": "unknown"}""")

false by default.