kotlin-serialization

pluginagentmarketplace's avatarfrom pluginagentmarketplace

kotlinx.serialization - JSON, Protobuf, custom serializers

⭐1stars🔀0forks📁View on GitHub🕐Updated Dec 31, 2025

When & Why to Use This Skill

This Claude skill provides comprehensive support for kotlinx.serialization, the official Kotlin multiplatform serialization library. It enables developers to perform type-safe conversion between Kotlin objects and various data formats including JSON, Protobuf, and CBOR. The skill covers essential patterns such as custom serializers, polymorphic serialization for sealed classes, and troubleshooting common configuration issues, significantly streamlining data handling in Kotlin-based applications.

Use Cases

  • REST API Development: Seamlessly encode and decode JSON payloads for web services using type-safe Kotlin data classes and @Serializable annotations.
  • High-Performance Messaging: Implement efficient binary data transfer using Protocol Buffers (Protobuf) for microservices or mobile-to-server communication.
  • Complex State Management: Handle polymorphic data structures and inheritance hierarchies using sealed classes to maintain type integrity during serialization.
  • Legacy Data Integration: Create custom KSerializer implementations to map non-standard or third-party data types (like Instant or UUID) to specific storage formats.
namekotlin-serialization
descriptionkotlinx.serialization - JSON, Protobuf, custom serializers
version"1.0.0"
sasmp_version"1.3.0"
bonded_agent01-kotlin-fundamentals
bond_typeSECONDARY_BOND
timeout_ms30000
max_attempts3
backoffexponential
initial_delay_ms1000
- nameversion
typestring
validation"^(json|protobuf|cbor|custom)$"
default"1.6.3"
levelinfo
events[skill_invoked, format_loaded, error_occurred]

Kotlin Serialization Skill

Type-safe serialization with kotlinx.serialization.

Topics Covered

JSON Serialization

@Serializable
data class User(
    val id: Long,
    val name: String,
    @SerialName("email_address") val email: String,
    val createdAt: Instant = Instant.now()
)

val json = Json {
    ignoreUnknownKeys = true
    encodeDefaults = true
    prettyPrint = true
}

val user = json.decodeFromString<User>(jsonString)
val output = json.encodeToString(user)

Custom Serializers

object InstantSerializer : KSerializer<Instant> {
    override val descriptor = PrimitiveSerialDescriptor("Instant", PrimitiveKind.LONG)
    override fun serialize(encoder: Encoder, value: Instant) = encoder.encodeLong(value.toEpochMilli())
    override fun deserialize(decoder: Decoder) = Instant.ofEpochMilli(decoder.decodeLong())
}

@Serializable
data class Event(
    @Serializable(with = InstantSerializer::class) val timestamp: Instant
)

Polymorphic Serialization

@Serializable
sealed class Response {
    @Serializable @SerialName("success")
    data class Success(val data: String) : Response()

    @Serializable @SerialName("error")
    data class Error(val message: String) : Response()
}

val json = Json { classDiscriminator = "type" }

Troubleshooting

Issue Resolution
"Serializer not found" Add @Serializable or plugin
Unknown property fails Set ignoreUnknownKeys = true

Usage

Skill("kotlin-serialization")