Basic Serialization
This page covers basic serialization patterns and Fory instance creation.
Creating Fory Instances
Single-Thread Fory
For single-threaded applications:
Fory fory = Fory.builder()
.withLanguage(Language.JAVA)
// enable reference tracking for shared/circular reference.
// Disable it will have better performance if no duplicate reference.
.withRefTracking(false)
.withCompatibleMode(CompatibleMode.SCHEMA_CONSISTENT)
// enable type forward/backward compatibility
// disable it for small size and better performance.
// .withCompatibleMode(CompatibleMode.COMPATIBLE)
// enable async multi-threaded compilation.
.withAsyncCompilation(true)
.build();
byte[] bytes = fory.serialize(object);
System.out.println(fory.deserialize(bytes));
Thread-Safe Fory
For multi-threaded applications:
ThreadSafeFory fory = Fory.builder()
.withLanguage(Language.JAVA)
// enable reference tracking for shared/circular reference.
// Disable it will have better performance if no duplicate reference.
.withRefTracking(false)
// compress int for smaller size
// .withIntCompressed(true)
// compress long for smaller size
// .withLongCompressed(true)
.withCompatibleMode(CompatibleMode.SCHEMA_CONSISTENT)
// enable type forward/backward compatibility
// disable it for small size and better performance.
// .withCompatibleMode(CompatibleMode.COMPATIBLE)
// enable async multi-threaded compilation.
.withAsyncCompilation(true)
.buildThreadSafeFory();
byte[] bytes = fory.serialize(object);
System.out.println(fory.deserialize(bytes));
Object Deep Copy
Fory provides efficient deep copy functionality:
With Reference Tracking
Fory fory = Fory.builder().withRefCopy(true).build();
SomeClass a = xxx;
SomeClass copied = fory.copy(a);
Without Reference Tracking (Better Performance)
When disabled, deep copy will ignore circular and shared references. Same reference of an object graph will be copied into different objects in one Fory#copy:
Fory fory = Fory.builder().withRefCopy(false).build();
SomeClass a = xxx;
SomeClass copied = fory.copy(a);
Serialization APIs
Basic Serialize/Deserialize
// Serialize object to byte array
byte[] bytes = fory.serialize(object);
// Deserialize byte array to object
Object obj = fory.deserialize(bytes);
Serialize/Deserialize with Type
// Serialize with explicit type
byte[] bytes = fory.serializeJavaObject(object);
// Deserialize with expected type
MyClass obj = fory.deserializeJavaObject(bytes, MyClass.class);
Serialize/Deserialize with Type Info
// Serialize with type information
byte[] bytes = fory.serializeJavaObjectAndClass(object);
// Deserialize with embedded type info
Object obj = fory.deserializeJavaObjectAndClass(bytes);
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: Register frequently used classes for better performance
- Configure reference tracking: Disable if you don't have circular/shared references
Related Topics
- Configuration Options - All ForyBuilder options
- Type Registration - Class registration
- Troubleshooting - Common API usage issues