配置
本页介绍 Fory 配置选项和序列化模式。
序列化模式
Apache Fory™ 支持两种序列化模式:
SchemaConsistent 模式(默认)
类型声明必须在通信双方完全匹配:
auto fory = Fory::builder().build(); // 默认为 SchemaConsistent
Compatible 模式
允许独立的 schema 演化:
auto fory = Fory::builder().compatible(true).build();
构建器模式
使用 ForyBuilder 构造具有自定义配置的 Fory 实例:
#include "fory/serialization/fory.h"
using namespace fory::serialization;
// 默认配置
auto fory = Fory::builder().build();
// 用于 schema 演化的兼容模式
auto fory = Fory::builder()
.compatible(true)
.build();
// 跨语言模式
auto fory = Fory::builder()
.xlang(true)
.build();
// 完整配置
auto fory = Fory::builder()
.compatible(true)
.xlang(true)
.track_ref(true)
.max_dyn_depth(10)
.check_struct_version(true)
.build();
配置选项
xlang(bool)
启用/禁用跨语言(xlang)序列化模式。
auto fory = Fory::builder()
.xlang(true) // 启用跨语言兼容性
.build();
启用后,包含用于与 Java、Python、Go、Rust 和 JavaScript 跨语言兼容的元数据。
默认值: true
compatible(bool)
启用/禁用用于 schema 演化的兼容模式。
auto fory = Fory::builder()
.compatible(true) // 启用 schema 演化
.build();
启用后,支持读取使用不同 schema 版本序列化的数据。
默认值: false
track_ref(bool)
启用/禁用共享引用和循环引用的引用跟踪。
auto fory = Fory::builder()
.track_ref(true) // 启用引用跟踪
.build();
启用后,避免重复序列化共享对象并处理循环引用。
默认值: true
max_dyn_depth(uint32_t)
设置动态类型对象 的最大允许嵌套深度。
auto fory = Fory::builder()
.max_dyn_depth(10) // 允许最多 10 层
.build();