Skip to main content
Version: dev

Kotlin Setup

Fory Kotlin provides binary Object Serialization, generated models, Fory gRPC, and Android support. It runs on Fory Java and supports Java 8 and later.

Verify the Toolchain

java -version
./gradlew --version
# or: mvn -version

Object Serialization

Add the Fory Kotlin library to the application module:

build.gradle.kts
dependencies {
implementation("org.apache.fory:fory-kotlin:1.5.0")
}

Create src/main/kotlin/KotlinExample.kt:

import org.apache.fory.ThreadSafeFory
import org.apache.fory.kotlin.ForyKotlin

data class User(val id: Long, val name: String)

fun main() {
val fory: ThreadSafeFory = ForyKotlin.builder()
.withXlang(true)
.requireClassRegistration(true)
.buildThreadSafeFory()
fory.register(User::class.java, 1)

val bytes = fory.serialize(User(1, "Alice"))
val decoded = fory.deserialize(bytes) as User
println(decoded.name)
}

If the project applies Gradle's application plugin, run its application task:

./gradlew run

Use xlang mode when a peer uses a different Fory implementation family; use native mode for data within the JVM Fory implementation family. Continue with Kotlin Object Serialization, xlang, or native mode.

Other Capabilities