Kotlin Xlang Serialization
Kotlin xlang serialization uses the JVM Fory implementation through ForyKotlin. Use it when
Kotlin payloads must be read by another supported Fory runtime. 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 an xlang 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}")
}