Configuration
This page covers Kotlin-specific runtime configuration and Fory instance creation.
Xlang Setup
Fory Kotlin follows the Java builder default: xlang mode with compatible schema evolution. Use this path for cross-language Kotlin payloads, schema IDL generated Kotlin models, and KSP-generated xlang serializers.
import org.apache.fory.kotlin.ForyKotlin
val fory = ForyKotlin.builder()
.withXlang(true)
.requireClassRegistration(true)
.build()
Native Mode Setup
For same-language Kotlin/JVM payloads that need native JVM object behavior, use native mode explicitly:
import org.apache.fory.kotlin.ForyKotlin
val fory = ForyKotlin.builder().withXlang(false)
.requireClassRegistration(true)
.build()
Thread Safety
Fory instance creation is not cheap. Instances should be shared between multiple serializations.
Single-Thread Usage
import org.apache.fory.Fory
import org.apache.fory.kotlin.ForyKotlin
object ForyHolder {
val fory: Fory = ForyKotlin.builder()
.withXlang(true)
.requireClassRegistration(true)
.build()
}
Multi-Thread Usage
For multi-threaded applications, use ThreadSafeFory:
import org.apache.fory.ThreadSafeFory
import org.apache.fory.kotlin.ForyKotlin
object ForyHolder {
val fory: ThreadSafeFory = ForyKotlin.builder()
.withXlang(true)
.requireClassRegistration(true)
.buildThreadSafeFory()
}
Using Builder Methods
// Thread-safe Fory
val fory: ThreadSafeFory = ForyKotlin.builder()
.withXlang(true)
.requireClassRegistration(true)
.buildThreadSafeFory()
Configuration
All configuration options from Fory Java are available. See Java Configuration for the complete list.
Common options for Kotlin native-mode payloads:
import org.apache.fory.kotlin.ForyKotlin
val fory = ForyKotlin.builder().withXlang(false)
// Enable reference tracking for circular references
.withRefTracking(true)
// Enable schema evolution support for native-mode payloads
.withCompatible(true)
// Enable async compilation for better startup performance
.withAsyncCompilation(true)
// Compression options
.withIntCompressed(true)
.withLongCompressed(true)
.build()
Security
Kotlin uses the Java runtime configuration surface. Keep class registration enabled for production and any untrusted payload source:
val fory = ForyKotlin.builder()
.requireClassRegistration(true)
.withMaxDepth(50)
.build()
Security-related configuration:
- Keep
requireClassRegistration(true)and register application classes or generated modules. - Use
withMaxDepth(...)to reject unexpectedly deep object graphs. - Follow Java Configuration for allow-listing and unknown-class controls.