Skip to main content
Version: dev

Basic Serialization

Xlang is the default serialization mode for Fory Kotlin. This page covers the basic serialization API and interoperability rules for that default mode.

Cross-Language Interoperability

The following sections cover model generation, registration, and cross-language round trips in the default xlang mode.

Kotlin xlang serialization uses the JVM Fory implementation through ForyKotlin. Use it when Kotlin payloads must be read by another supported Fory implementation. Register portable model types with the same identity and field schema on every peer.

Kotlin data classes, enums, and sealed-class models use the Kotlin integration and generated serializers where applicable. Exact portable carrier mappings remain defined by the xlang type mapping.

Create a Fory Instance

import org.apache.fory.kotlin.ForyKotlin

val fory = ForyKotlin.builder()
.withXlang(true)
.build()

First round trip

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

data class Person(val name: String, val age: Int)

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

val bytes = fory.serialize(Person("chaokunyang", 28))
val result = fory.deserialize(bytes) as Person
println("${result.name} ${result.age}")
}