pub struct ForyBuilder { /* private fields */ }Expand description
Builder for configuring a Fory runtime before first use.
ForyBuilder owns the configuration phase. Call build to create the
runtime, then use Fory for registration and serialization operations.
use fory_core::Fory;
let fory = Fory::builder()
.xlang(true)
.compress_string(true)
.max_dyn_depth(10)
.build();Implementations§
Source§impl ForyBuilder
impl ForyBuilder
Sourcepub fn compatible(self, compatible: bool) -> ForyBuilder
pub fn compatible(self, compatible: bool) -> ForyBuilder
Sets the serialization compatible mode for this Fory builder.
§Arguments
compatible- The serialization compatible mode to use. Options are:false: Schema must be consistent between serialization and deserialization. No metadata is shared. This is the fastest mode.true: Supports schema evolution and type metadata sharing for better cross-version compatibility.
§Returns
Returns self for method chaining.
§Note
Setting the compatible mode also automatically configures the share_meta flag:
false→share_meta = falsetrue→share_meta = true
§Examples
use fory_core::Fory;
let fory = Fory::builder().xlang(true).compatible(true).build();Sourcepub fn xlang(self, xlang: bool) -> ForyBuilder
pub fn xlang(self, xlang: bool) -> ForyBuilder
Enables or disables xlang mode.
§Arguments
xlang- Iftrue, uses the xlang wire format compatible with other Fory implementations (Java, Python, C++, etc.). Iffalse, uses Rust native mode.
§Returns
Returns self for method chaining.
§Default
The default value is true.
§Examples
use fory_core::Fory;
// Xlang mode, the default cross-language wire format
let fory = Fory::builder().xlang(true).build();
// Native mode for Rust-only traffic
let fory = Fory::builder().xlang(false).build();Sourcepub fn compress_string(self, compress_string: bool) -> ForyBuilder
pub fn compress_string(self, compress_string: bool) -> ForyBuilder
Enables or disables meta string compression.
§Arguments
compress_string- Iftrue, enables meta string compression to reduce serialized payload size by deduplicating and encoding frequently used strings (such as type names and field names). Iffalse, strings are serialized without compression.
§Returns
Returns self for method chaining.
§Default
The default value is false.
§Trade-offs
- Enabled: Smaller payload size, slightly higher CPU overhead
- Disabled: Larger payload size, faster serialization/deserialization
§Examples
use fory_core::Fory;
let fory = Fory::builder().xlang(true).compress_string(true).build();Sourcepub fn check_string_read(self, check_string_read: bool) -> ForyBuilder
pub fn check_string_read(self, check_string_read: bool) -> ForyBuilder
Enables or disables checked UTF-8 string reads.
Checked reads validate UTF-8 payload bytes before constructing Rust String values.
Disabling this keeps the faster unchecked construction path and must only be used when
serialized bytes are trusted to contain valid UTF-8 strings.
§Default
The default value is true.
Sourcepub fn check_struct_version(self, check_struct_version: bool) -> ForyBuilder
pub fn check_struct_version(self, check_struct_version: bool) -> ForyBuilder
Enables or disables class version checking for schema consistency.
§Arguments
check_struct_version- Iftrue, enables class version checking to ensure schema consistency between serialization and deserialization. When enabled, a version hash computed from field types is written/read to detect schema mismatches. Iffalse, no version checking is performed.
§Returns
Returns self for method chaining.
§Default
The default value is false.
§Note
This feature is only effective when compatible mode is false. In compatible mode,
schema evolution is supported and version checking is not needed.
§Examples
use fory_core::Fory;
let fory = Fory::builder().xlang(false)
.compatible(false)
.check_struct_version(true)
.build();Sourcepub fn track_ref(self, track_ref: bool) -> ForyBuilder
pub fn track_ref(self, track_ref: bool) -> ForyBuilder
Enables or disables reference tracking for shared and circular references.
§Arguments
track_ref- Iftrue, enables reference tracking which allows preserving shared object references and circular references during serialization/deserialization.
§Returns
Returns self for method chaining.
§Default
The default value is false.
§Examples
use fory_core::Fory;
let fory = Fory::builder().xlang(true).track_ref(true).build();Sourcepub fn max_dyn_depth(self, max_dyn_depth: u32) -> ForyBuilder
pub fn max_dyn_depth(self, max_dyn_depth: u32) -> ForyBuilder
Sets the maximum depth for nested dynamic object serialization.
§Arguments
max_dyn_depth- The maximum nesting depth allowed for dynamically-typed objects (e.g., trait objects, boxed types). This prevents stack overflow from deeply nested structures in dynamic serialization scenarios.
§Returns
Returns self for method chaining.
§Default
The default value is 5.
§Behavior
When the depth limit is exceeded during deserialization, an error is returned to prevent potential stack overflow or infinite recursion.
§Examples
use fory_core::Fory;
// Allow deeper nesting for complex object graphs
let fory = Fory::builder().xlang(true).max_dyn_depth(10).build();
// Restrict nesting for safer deserialization
let fory = Fory::builder().xlang(true).max_dyn_depth(3).build();Sourcepub fn max_binary_size(self, max_binary_size: u32) -> ForyBuilder
pub fn max_binary_size(self, max_binary_size: u32) -> ForyBuilder
Sets the maximum allowed size for binary data during deserialization.
§Arguments
max_binary_size- The maximum number of bytes allowed for a single binary/primitive-array payload during deserialization. Payloads exceeding this limit will cause aSizeLimitExceedederror.
§Returns
Returns self for method chaining.
§Default
The default value is 64 * 1024 * 1024 (64 MB).
§Examples
use fory_core::Fory;
// Limit binary payloads to 1 MB
let fory = Fory::builder().xlang(true).max_binary_size(1024 * 1024).build();Sourcepub fn max_collection_size(self, max_collection_size: u32) -> ForyBuilder
pub fn max_collection_size(self, max_collection_size: u32) -> ForyBuilder
Sets the maximum allowed number of elements in a collection or entries in a map during deserialization.
§Arguments
max_collection_size- The maximum number of elements/entries allowed for a single collection or map during deserialization. Payloads exceeding this limit will cause aSizeLimitExceedederror.
§Returns
Returns self for method chaining.
§Default
The default value is 1024 * 1024 (1 million elements).
§Examples
use fory_core::Fory;
// Limit collections to 10000 elements
let fory = Fory::builder().xlang(true).max_collection_size(10000).build();