Skip to main content
Version: dev

Kotlin Serialization Guide

Apache Fory™ Kotlin provides optimized serializers for Kotlin types, built on top of Fory Java. Most standard Kotlin types work out of the box with the default Fory Java implementation, while Fory Kotlin adds additional support for Kotlin-specific types.

Supported types include:

  • data class serialization
  • Unsigned primitives: UByte, UShort, UInt, ULong
  • Unsigned arrays: UByteArray, UShortArray, UIntArray, ULongArray
  • Stdlib types: Pair, Triple, Result
  • Ranges: IntRange, LongRange, CharRange, and progressions
  • Collections: ArrayDeque, empty collections (emptyList, emptyMap, emptySet)
  • kotlin.time.Duration, kotlin.text.Regex, kotlin.uuid.Uuid

Features

Fory Kotlin inherits all features from Fory Java, plus Kotlin-specific optimizations:

  • High Performance: JIT code generation, zero-copy, 20-170x faster than traditional serialization
  • Kotlin Type Support: Optimized serializers for data classes, unsigned types, ranges, and stdlib types
  • Default Value Support: Automatic handling of Kotlin data class default parameters during schema evolution
  • Schema Evolution: Forward/backward compatibility for class schema changes

See Java Features for complete feature list.

Installation

Maven

<dependency>
<groupId>org.apache.fory</groupId>
<artifactId>fory-kotlin</artifactId>
<version>0.14.1</version>
</dependency>

Gradle

implementation("org.apache.fory:fory-kotlin:0.14.1")

Quick Start

import org.apache.fory.Fory
import org.apache.fory.ThreadSafeFory
import org.apache.fory.serializer.kotlin.KotlinSerializers

data class Person(val name: String, val id: Long, val github: String)
data class Point(val x: Int, val y: Int, val z: Int)

fun main() {
// Create Fory instance (should be reused)
val fory: ThreadSafeFory = Fory.builder()
.requireClassRegistration(true)
.buildThreadSafeFory()

// Register Kotlin serializers
KotlinSerializers.registerSerializers(fory)

// Register your classes
fory.register(Person::class.java)
fory.register(Point::class.java)

val p = Person("Shawn Yang", 1, "https://github.com/chaokunyang")
println(fory.deserialize(fory.serialize(p)))
println(fory.deserialize(fory.serialize(Point(1, 2, 3))))
}

Built on Fory Java

Fory Kotlin is built on top of Fory Java. Most configuration options, features, and concepts from Fory Java apply directly to Kotlin. Refer to the Java documentation for:

Kotlin-Specific Documentation