跳到主要内容
版本:0.12

跨语言类型映射

注意:

  • 类型定义请参见 规范中的类型系统
  • int16_t[n]/vector<T> 表示 int16_t[n]/vector<int16_t>
  • 跨语言序列化功能尚不稳定,请勿在生产环境中使用。

类型映射

Fory TypeFory Type IDJavaPythonJavascriptC++GolangRust
bool1bool/BooleanboolBooleanboolboolbool
int82byte/Byteint/pyfory.Int8Type.int8()int8_tint8i8
int163short/Shortint/pyfory.Int16Type.int16()int16_tint16i6
int324int/Integerint/pyfory.Int32Type.int32()int32_tint32i32
var_int325int/Integerint/pyfory.VarInt32Type.varint32()fory::varint32_tfory.varint32fory::varint32
int646long/Longint/pyfory.Int64Type.int64()int64_tint64i64
var_int647long/Longint/pyfory.VarInt64Type.varint64()fory::varint64_tfory.varint64fory::varint64
sli_int648long/Longint/pyfory.SliInt64Type.sliint64()fory::sliint64_tfory.sliint64fory::sliint64
float169float/Floatfloat/pyfory.Float16Type.float16()fory::float16_tfory.float16fory::f16
float3210float/Floatfloat/pyfory.Float32Type.float32()floatfloat32f32
float6411double/Doublefloat/pyfory.Float64Type.float64()doublefloat64f64
string12StringstrStringstringstringString/str
enum13Enum subclassesenum subclasses/enum/enum
named_enum14Enum subclassesenum subclasses/enum/enum
struct15pojo/recorddata class / type with type hintsobjectstruct/classstructstruct
compatible_struct16pojo/recorddata class / type with type hintsobjectstruct/classstructstruct
named_struct17pojo/recorddata class / type with type hintsobjectstruct/classstructstruct
named_compatible_struct18pojo/recorddata class / type with type hintsobjectstruct/classstructstruct
ext19pojo/recorddata class / type with type hintsobjectstruct/classstructstruct
named_ext20pojo/recorddata class / type with type hintsobjectstruct/classstructstruct
list21List/Collectionlist/tuplearrayvectorsliceVec
set22Setset/setfory.SetSet
map23MapdictMapunordered_mapmapHashMap
duration24DurationtimedeltaNumberdurationDurationDuration
timestamp25InstantdatetimeNumberstd::chrono::nanosecondsTimeDateTime
local_date26DatedatetimeNumberstd::chrono::nanosecondsTimeDateTime
decimal27BigDecimalDecimalbigint///
binary28byte[]bytes/uint8_t[n]/vector<T>[n]uint8/[]TVec<uint8_t>
array29arraynp.ndarray//array/sliceVec
bool_array30bool[]ndarray(np.bool_)/bool[n][n]bool/[]TVec<bool>
int8_array31byte[]ndarray(int8)/int8_t[n]/vector<T>[n]int8/[]TVec<i18>
int16_array32short[]ndarray(int16)/int16_t[n]/vector<T>[n]int16/[]TVec<i16>
int32_array33int[]ndarray(int32)/int32_t[n]/vector<T>[n]int32/[]TVec<i32>
int64_array34long[]ndarray(int64)/int64_t[n]/vector<T>[n]int64/[]TVec<i64>
float16_array35float[]ndarray(float16)/fory::float16_t[n]/vector<T>[n]float16/[]TVec<fory::f16>
float32_array36float[]ndarray(float32)/float[n]/vector<T>[n]float32/[]TVec<f32>
float64_array37double[]ndarray(float64)/double[n]/vector<T>[n]float64/[]TVec<f64>
arrow record batch38//////
arrow table39//////

类型信息(当前未实现)

由于各语言的类型系统存在差异,某些类型无法做到一一映射。

如果用户发现某一语言的类型在 Fory 类型系统中对应多个类型,例如 Java 中的 long 对应 int64/varint64/sliint64,这意味着该语言缺少某些类型,用户在使用 Fory 时需要额外提供类型信息。

类型注解

如果类型是另一个类的字段,用户可以为类型的字段或整个类型提供元信息提示。 这些信息在其他语言中也可以提供:

  • Java:使用 annotation。
  • C++:使用宏和模板。
  • Golang:使用 struct tag。
  • Python:使用 typehint。
  • Rust:使用宏。

示例:

  • Java:

    class Foo {
    @Int32Type(varint = true)
    int f1;
    List<@Int32Type(varint = true) Integer> f2;
    }
  • Python:

    class Foo:
    f1: Int32Type(varint=True)
    f2: List[Int32Type(varint=True)]

类型包装器

如果类型不是类的字段,用户必须用 Fory 类型包装该类型以传递额外的类型信息。

例如,假设 Fory Java 提供了 VarInt64 类型,当用户调用 fory.serialize(long_value) 时,需要这样调用:fory.serialize(new VarInt64(long_value))