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

This annotation has lesser priority than SerialName. In practice, this means that if property A has @SerialName("foo") annotation, and property B has @JsonNames("foo") annotation, Json key foo will be deserialized into property A.

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>