Basic Serialization
This page covers the Java xlang quickstart. Xlang mode is the default Java wire format and is the right first choice for cross-language payloads.
Create a Fory Instance
For a single-threaded xlang Fory instance, set the mode explicitly:
import org.apache.fory.Fory;
Fory fory = Fory.builder()
.withXlang(true)
.requireClassRegistration(true)
.build();
For a thread-safe Fory instance, build ThreadSafeFory from the same builder:
import org.apache.fory.ThreadSafeFory;
ThreadSafeFory fory = Fory.builder()
.withXlang(true)
.requireClassRegistration(true)
.buildThreadSafeFory();
Default Java xlang mode also defaults to compatible schema mode, so independently deployed services
can add and remove fields when their schema metadata remains compatible. Use
withCompatible(false) only when every reader and writer always uses the same schema and you want
faster serialization and smaller size. Use the compatible=false opt-out only after verifying that every language uses the same xlang schema, or when native types are generated from Fory schema IDL.
Register Custom Types
Register application classes with the same type identity on every peer. Numeric IDs are compact and fast, while name registration is easier to coordinate across independently owned services.
import org.apache.fory.annotation.ForyField;
public class User {
@ForyField(id = 0)
public String name;
@ForyField(id = 1)
public int age;
}
Fory fory = Fory.builder()
.withXlang(true)
.requireClassRegistration(true)
.build();
fory.register(User.class, "example", "User");
Use field IDs for long-lived schemas so field identity is stable even if Java field names change. See Schema Metadata for Java annotations, nullability, reference tracking, and enum metadata.
Serialize And Deserialize
User user = new User();
user.name = "Alice";
user.age = 30;
byte[] bytes = fory.serialize(user);
User decoded = fory.deserialize(bytes, User.class);
When xlang bytes cross languages, every peer must register the same type identity and compatible field metadata. The shared rules live in Xlang, while Java-specific API calls are in Xlang Serialization.
Use Native Serialization For Java-Only Traffic
For same-language Java/JVM traffic, native mode is usually the better fit:
Fory fory = Fory.builder()
.withXlang(false)
.build();
Native mode supports the broad Java object serialization surface, including JDK serialization hooks, object copy, and native-mode zero-copy buffers. See Native Serialization.
Common Options
withRefTracking(true)preserves shared references and circular references.requireClassRegistration(true)keeps the default registered-type policy.- Compatible mode is enabled by default for native-mode and xlang payloads. Use
withCompatible(false)only when every reader and writer uses the same schema and you want faster serialization and smaller size. For xlang payloads, use thecompatible=falseopt-out only after verifying that every language uses the same schema, or when native types are generated from Fory schema IDL. withAsyncCompilation(true)enables asynchronous serializer compilation where supported.
Best Practices
- Reuse Fory instances: Creating Fory is expensive, always reuse instances
- Use appropriate thread safety: Choose between single-thread and thread-safe based on your needs
- Register classes: Keep type identity stable across every xlang peer
- Configure reference tracking: Enable it only when the object graph needs identity or cycles
Related Topics
- Configuration - All ForyBuilder options
- Native Serialization - Java-only serialization features
- Schema Metadata - Field IDs, nullability, reference tracking, and enum IDs
- Xlang Serialization - Java xlang interoperability
- Troubleshooting - Common API usage issues