入门指南
本指南介绍所有受支持语言中跨语 言序列化的安装与基础设置。
安装
Java
Maven:
<dependency>
<groupId>org.apache.fory</groupId>
<artifactId>fory-core</artifactId>
<version>0.17.0</version>
</dependency>
Gradle:
implementation 'org.apache.fory:fory-core:0.17.0'
Python
pip install pyfory
Go
go get github.com/apache/fory/go/fory
Rust
[dependencies]
fory = "0.17.0"
JavaScript
npm install @apache-fory/fory
C++
使用 Bazel 或 CMake 从源码构建。详见 C++ 指南。
启用跨语言模式
每种语言都需要启用 xlang 模式,以确保跨语言之间的二进制兼容性。
Java
import org.apache.fory.*;
import org.apache.fory.config.*;
Fory fory = Fory.builder()
.withLanguage(Language.XLANG) // 启用跨语言模式
.withRefTracking(true) // 可选:用于循环引用
.build();
Python
import pyfory
# 必须显式启用跨语言模式
fory = pyfory.Fory(xlang=True)
# 需要时启用引用跟踪
fory = pyfory.Fory(xlang=True, ref=True)
Go
import forygo "github.com/apache/fory/go/fory"
fory := forygo.NewFory(forygo.WithXlang(true))
// 或启用引用跟踪
fory := forygo.NewFory(forygo.WithXlang(true), forygo.WithTrackRef(true))
Rust
use fory::Fory;
let fory = Fory::default().xlang(true);
JavaScript
import Fory from "@apache-fory/fory";
const fory = new Fory();
C++
#include "fory/serialization/fory.h"
using namespace fory::serialization;
auto fory = Fory::builder()
.xlang(true)
.build();
类型注册
自定义类型必须在所有语言中使用一致的名称或 ID 注册。
按名称注册(推荐)
使用字符串名称更灵活,也更不容易发生冲突:
Java:
fory.register(Person.class, "example.Person");
Python:
fory.register_type(Person, typename="example.Person")
Go:
fory.RegisterNamedStruct(Person{}, "example.Person")