CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jetbrains-kotlinx--kotlinx-serialization-core-jvm

Kotlin multiplatform serialization runtime library core module with JVM target support

Pending
Overview
Eval results
Files

annotations.mddocs/

Annotations

Essential annotations for controlling serialization behavior in kotlinx.serialization. These annotations allow you to mark classes as serializable, customize field names, control which properties are serialized, and configure advanced serialization features.

Capabilities

Core Annotations

The fundamental annotations needed for basic serialization.

/**
 * Marks a class as serializable, instructing the compiler plugin to generate a KSerializer implementation.
 * @param with Optional custom serializer class to use instead of generated one
 */
@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY, AnnotationTarget.TYPE)
annotation class Serializable(val with: KClass<out KSerializer<*>> = KSerializer::class)

/**
 * Overrides the name of a class or property in the SerialDescriptor.
 * Used to customize how fields appear in the serialized format.
 * @param value The custom name to use in serialization
 */
@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY)  
annotation class SerialName(val value: String)

/**
 * Marks a property as invisible to the entire serialization process.
 * Transient properties are completely ignored during serialization and deserialization.
 */
@Target(AnnotationTarget.PROPERTY)
annotation class Transient

Usage Examples:

@Serializable
data class User(
    @SerialName("user_name")
    val name: String,
    val email: String,
    @Transient
    val password: String = ""
)

// Serializes as: {"user_name":"Alice","email":"alice@example.com"}
// password field is completely ignored

Property Control Annotations

Annotations for fine-grained control over property serialization behavior.

/**
 * Marks a property as required during deserialization, even if it has a default value.
 * Throws MissingFieldException if the field is not present in input.
 */
@Target(AnnotationTarget.PROPERTY)
annotation class Required

/**
 * Controls whether a property is serialized when its value equals the default value.
 * @param mode Strategy for encoding default values
 */
@Target(AnnotationTarget.PROPERTY)
annotation class EncodeDefault(val mode: Mode = Mode.ALWAYS) {
    enum class Mode {
        /**
         * Configures serializer to always encode the property, even if its value is equal to its default.
         * For annotated properties, format settings are not taken into account.
         */
        ALWAYS,
        
        /**
         * Configures serializer not to encode the property if its value is equal to its default.
         * For annotated properties, format settings are not taken into account.
         */
        NEVER
    }
}

Usage Examples:

@Serializable
data class Config(
    @Required
    val apiKey: String = "", // Always required, even with default
    
    @EncodeDefault(EncodeDefault.Mode.ALWAYS)
    val timeout: Int = 30,   // Always serialized, even if 30
    
    @EncodeDefault(EncodeDefault.Mode.NEVER)
    val debug: Boolean = false // Only serialized if true
)

Custom Serializer Annotations

Annotations for specifying and managing custom serializers.

/**
 * Instructs the plugin to turn the annotated class into a serializer for the specified target class.
 * @param forClass The class this serializer handles
 */
@Target(AnnotationTarget.CLASS)
annotation class Serializer(val forClass: KClass<*>)

/**
 * Instructs the plugin to use ContextualSerializer for the annotated property.
 * The actual serializer is resolved from SerializersModule at runtime.
 */
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.TYPE)
annotation class Contextual

/**
 * Keep the auto-generated KSerializer even when a custom serializer is specified.
 * The generated serializer is available via generatedSerializer() function.
 */
@Target(AnnotationTarget.CLASS)
annotation class KeepGeneratedSerializer

Usage Examples:

@Serializer(forClass = LocalDate::class)
object LocalDateSerializer : KSerializer<LocalDate> {
    override val descriptor = PrimitiveSerialDescriptor("LocalDate", PrimitiveKind.STRING)
    override fun serialize(encoder: Encoder, value: LocalDate) = encoder.encodeString(value.toString())
    override fun deserialize(decoder: Decoder): LocalDate = LocalDate.parse(decoder.decodeString())
}

@Serializable
data class Event(
    val name: String,
    @Contextual 
    val date: LocalDate  // Resolved from SerializersModule
)

File-Level Configuration Annotations

Annotations that apply to entire files or modules for bulk configuration.

/**
 * Use ContextualSerializer for every type listed in forClasses within the annotated file.
 * @param forClasses Array of classes to treat as contextual
 */
@Target(AnnotationTarget.FILE)
annotation class UseContextualSerialization(vararg val forClasses: KClass<*>)

/**
 * Adds the specified serializer classes to the serialization resolution process for the file.
 * @param serializerClasses Array of serializer classes to register
 */
@Target(AnnotationTarget.FILE)  
annotation class UseSerializers(vararg val serializerClasses: KClass<out KSerializer<*>>)

/**
 * Instructs the plugin to use PolymorphicSerializer for the annotated property or type.
 * Used for polymorphic serialization scenarios.
 */
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS, AnnotationTarget.TYPE)
annotation class Polymorphic

Usage Examples:

@file:UseContextualSerialization(LocalDate::class, UUID::class)
@file:UseSerializers(LocalDateSerializer::class, UUIDSerializer::class)

package com.example.models

@Serializable
data class Document(
    val id: UUID,        // Uses UUIDSerializer from file-level config
    val created: LocalDate  // Uses LocalDateSerializer from file-level config
)

Meta-Annotations

Annotations for creating custom serialization annotations.

/**
 * Meta-annotation for creating custom serialization-specific annotations.
 * Marks an annotation as containing serialization metadata.
 */
@Target(AnnotationTarget.ANNOTATION_CLASS)
annotation class SerialInfo

/**
 * Like SerialInfo but makes the annotation inheritable by subclasses.
 * Useful for creating annotation hierarchies.
 */
@Target(AnnotationTarget.ANNOTATION_CLASS)
annotation class InheritableSerialInfo

/**
 * Meta-annotation that adds Serializable behavior to user-defined annotations.
 * Classes annotated with meta-serializable annotations become serializable.
 */
@Target(AnnotationTarget.ANNOTATION_CLASS)
annotation class MetaSerializable

Usage Examples:

@SerialInfo
@Target(AnnotationTarget.PROPERTY)
annotation class Since(val version: String)

@InheritableSerialInfo  
@Target(AnnotationTarget.CLASS)
annotation class ApiVersion(val version: Int)

@MetaSerializable
@Target(AnnotationTarget.CLASS)
annotation class JsonSerializable

// Classes with JsonSerializable automatically become @Serializable
@JsonSerializable
data class User(
    @Since("1.0")
    val name: String,
    @Since("1.1") 
    val email: String
)

Error Handling

Annotation-related errors typically occur at compile time when the kotlinx.serialization compiler plugin processes the annotations. Common issues include:

  • Missing @Serializable annotation on classes used in serializable properties
  • Invalid @SerialName values that conflict with other field names
  • Incorrect @Serializer configurations pointing to non-existent classes
  • Circular dependencies in @Contextual or polymorphic type hierarchies

Install with Tessl CLI

npx tessl i tessl/maven-org-jetbrains-kotlinx--kotlinx-serialization-core-jvm

docs

annotations.md

built-ins.md

core-serialization.md

descriptors.md

encoding.md

index.md

modules.md

tile.json