Configuration
This page covers Fory configuration options and serialization modes.
Serialization Modes
Apache Fory™ supports two serialization modes:
Compatible Mode (xlang default)
Compatible mode is recommended for cross-language services because schemas can diverge more easily across languages:
let fory = Fory::builder().xlang(true).compatible(true).build();
SchemaConsistent Mode
Type declarations must match exactly between peers:
let fory = Fory::builder().xlang(true).compatible(false).build();
Compatible Mode
For native Rust-only payloads, compatible mode is still explicit:
let fory = Fory::builder().compatible(true).build();
Configuration
Maximum Dynamic Object Nesting Depth
Apache Fory™ provides protection against stack overflow from deeply nested dynamic objects during deserialization. By default, the maximum nesting depth is set to 5 levels for trait objects and containers.
Default configuration:
let fory = Fory::default(); // max_dyn_depth = 5
Custom depth limit:
let fory = Fory::builder().max_dyn_depth(10).build(); // Allow up to 10 levels
When to adjust:
- Increase: For legitimate deeply nested data structures
- Decrease: For stricter security requirements or shallow data structures
Protected types:
Box<dyn Any>,Rc<dyn Any>,Arc<dyn Any>Box<dyn Trait>,Rc<dyn Trait>,Arc<dyn Trait>(trait objects)RcWeak<T>,ArcWeak<T>- Collection types (Vec, HashMap, HashSet)
- Nested struct types in Compatible mode
Note: Static data types (non-dynamic types) are secure by nature and not subject to depth limits, as their structure is known at compile time.
Cross-Language Mode
Enable cross-language serialization:
let fory = Fory::builder()
.compatible(true)
.xlang(true).build();
Builder Pattern
use fory::Fory;
// Default configuration
let fory = Fory::default();
// Compatible mode for schema evolution
let fory = Fory::builder().compatible(true).build();
// Cross-language mode
let fory = Fory::builder()
.compatible(true)
.xlang(true).build();
// Custom depth limit
let fory = Fory::builder().max_dyn_depth(10).build();
// Combined configuration
let fory = Fory::builder()
.compatible(true)
.xlang(true)
.max_dyn_depth(10).build();
Configuration Summary
| Option | Description | Default |
|---|---|---|
compatible(bool) | Enable schema evolution | false |
xlang(bool) | Enable cross-language mode | false |
max_dyn_depth(u32) | Maximum nesting depth for dynamic types | 5 |
Related Topics
- Basic Serialization - Using configured Fory
- Schema Evolution - Compatible mode details
- Cross-Language - XLANG mode