Get started

JsonNames

Indicates that the field can be represented in JSON with multiple possible alternative names. Json format recognizes this annotation and is able to decode the data using any of the alternative names.

Unlike SerialName annotation, does not affect JSON encoding in any way.

Example of usage:

@Serializable
data class Project(@JsonNames("title") val name: String)

val project = Json.decodeFromString<Project>("""{"name":"kotlinx.serialization"}""")
println(project) // OK
val oldProject = Json.decodeFromString<Project>("""{"title":"kotlinx.coroutines"}""")
println(oldProject) // Also OK

Specifying this annotation and SerialName with the same value leads to unspecified behavior, which is implementation-defined. In practice, this means that if property A has @SerialName("foo") annotation, and property B has @JsonNames("foo") annotation, Json key foo can be deserialized to the last property in the stream, set both properties, or throw an exception about duplicate keys. This behavior may differ across runtime versions.

Using the same alternative name for different properties across one class is prohibited and leads to a deserialization exception.

See also

Properties

Link copied to clipboard
val names: Array<out String>