SerialName

Overrides the name of a class or a property in the corresponding SerialDescriptor. Names and serial names are used by text-based serial formats in order to encode the name of the class or the name of the property, e.g. by Json.

By default, SerialDescriptor.serialName and SerialDescriptor.getElementName are associated with fully-qualified name of the target class and the name of the property respectively. Applying this annotation changes the visible name to the given value:

package foo

@Serializable // RegularName.serializer().descriptor.serialName is "foo.RegularName"
class RegularName(val myInt: Int)

@Serializable
@SerialName("CustomName") // Omit package from name that is used for diagnostic and polymorphism
class CustomName(@SerialName("int") val myInt: Int)

// Prints "{"myInt":42}"
println(Json.encodeToString(RegularName(42)))
// Prints "{"int":42}"
println(Json.encodeToString(CustomName(42)))

@Target(allowedTargets = [AnnotationTarget.PROPERTY, AnnotationTarget.CLASS])
annotation class SerialName(value: String)

Constructors

SerialName
Link copied to clipboard
common
fun SerialName(value: String)

Functions

equals
Link copied to clipboard
common
open operator override fun equals(other: Any?): Boolean
hashCode
Link copied to clipboard
common
open override fun hashCode(): Int
toString
Link copied to clipboard
common
open override fun toString(): String

Properties

value
Link copied to clipboard
common
val value: String