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 Runtime
For a single-threaded xlang runtime, set the mode explicitly:
import org.apache.fory.Fory;
Fory fory = Fory.builder()
.withXlang(true)
.requireClassRegistration(true)
.build();
For a thread-safe runtime, 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 peer updates schema together and you want schema-consistent
xlang payloads.
Register Custom Types
Register application classes with the same type identity on every peer. Numeric IDs are compact and fast, while namespace/type-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 runtimes, every runtime 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.withCompatible(true)enables compatible mode explicitly for native-mode schema evolution. Xlang mode already uses compatible mode by default.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