跳到主要内容
版本:dev

Configuration

This page covers Fory configuration options and serialization modes.

Serialization Modes

Apache Fory™ supports two serialization modes:

SchemaConsistent Mode (Default)

Type declarations must match exactly between peers:

let fory = Fory::default(); // SchemaConsistent by default

Compatible Mode

Allows independent schema evolution:

let fory = Fory::default().compatible(true);

Configuration Options

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::default().max_dyn_depth(10); // 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::default()
.compatible(true)
.xlang(true);

Builder Pattern

use fory::Fory;

// Default configuration
let fory = Fory::default();

// Compatible mode for schema evolution
let fory = Fory::default().compatible(true);

// Cross-language mode
let fory = Fory::default()
.compatible(true)
.xlang(true);

// Custom depth limit
let fory = Fory::default().max_dyn_depth(10);

// Combined configuration
let fory = Fory::default()
.compatible(true)
.xlang(true)
.max_dyn_depth(10);

Configuration Summary

OptionDescriptionDefault
compatible(bool)Enable schema evolutionfalse
xlang(bool)Enable cross-language modefalse
max_dyn_depth(u32)Maximum nesting depth for dynamic types5