Skip to main content
Version: 1.7

Kotlin Setup

Fory Kotlin provides binary Object Serialization, standard JSON mapping, 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​

The repositories below resolve release coordinates from Maven Central and -SNAPSHOT coordinates from the Apache snapshot repository. Keep every Fory module on the same version:

build.gradle.kts
repositories {
maven("https://repository.apache.org/snapshots/") {
mavenContent { snapshotsOnly() }
}
mavenCentral()
}

dependencies {
implementation("org.apache.fory:fory-kotlin:1.7.7")
}

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.

Standard JSON​

Fory JSON is a separate text format from binary Object Serialization. Add its optional Kotlin module when interoperating with ordinary JSON APIs, browsers, logs, or other JSON libraries:

build.gradle.kts
dependencies {
implementation("org.apache.fory:fory-json-kotlin:1.7.7")
}
import org.apache.fory.json.kotlin.ForyJsonKotlin
import org.apache.fory.json.kotlin.jsonTypeRef

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

val json = ForyJsonKotlin.builder().build()
val userType = jsonTypeRef<User>()
val text = json.toJson(User(1, "Alice"), userType)
val decoded = json.fromJson(text, userType)

The runtime reads Kotlin/JVM metadata directly. Add fory-json-kotlin-ksp only to Android builds that use R8 or ProGuard; it emits exact retention rules for Kotlin @JsonType models and source-owned exact Mixins. Native Image uses the normal @ForyJsonProvider workflow. Continue with Kotlin JSON.

Other Capabilities​