Skip to main content
Version: 1.0.0

Type Registration

This page explains how to register types for serialization.

Overview

Apache Fory™ requires explicit type registration for struct types. This design enables:

  • Xlang compatibility: Registered type IDs are used across language boundaries
  • Type Safety: Detects type mismatches at deserialization time
  • Polymorphic Serialization: Enables serialization of polymorphic objects via smart pointers

Registering Structs

Use register_struct<T>(type_id) to register a struct type:

#include "fory/serialization/fory.h"

using namespace fory::serialization;

struct Person {
std::string name;
int32_t age;
};
FORY_STRUCT(Person, name, age);

int main() {
auto fory = Fory::builder().xlang(true).build();

// Register with a unique type ID
fory.register_struct<Person>(100);

Person person{"Alice", 30};
auto bytes = fory.serialize(person).value();
auto decoded = fory.deserialize<Person>(bytes).value();
}

Type ID Guidelines

Type IDs must be:

  1. Unique: Each type must have a unique ID within a Fory instance
  2. Consistent: Same ID must be used across all languages and versions

User-registered type IDs are in a separate namespace from built-in type IDs, so you can start from 0:

// User type IDs can start from 0
fory.register_struct<Address>(0);
fory.register_struct<Person>(1);
fory.register_struct<Order>(2);

Registering Enums

Use register_enum<T>(type_id) to register an enum type. For simple enums with continuous values starting from 0, no macro is needed:

// Simple continuous enum - no FORY_ENUM needed
enum class Color { RED, GREEN, BLUE }; // Values: 0, 1, 2

// Register with register_enum
fory.register_enum<Color>(0);

For enums with non-continuous values, use the FORY_ENUM macro to map values to ordinals:

// Non-continuous enum values - FORY_ENUM required
enum class Priority { LOW = 10, MEDIUM = 50, HIGH = 100 };
FORY_ENUM(Priority, LOW, MEDIUM, HIGH);
// FORY_ENUM must be defined at namespace scope.

// Global namespace enum (prefix with ::)
enum SparseStatus { UNKNOWN = -1, OK = 0, ERROR = 1 };
FORY_ENUM(::SparseStatus, UNKNOWN, OK, ERROR);

// Register after FORY_ENUM
fory.register_enum<Priority>(1);
fory.register_enum<SparseStatus>(2);

When to use FORY_ENUM:

  • Enum values don't start from 0
  • Enum values are not continuous (e.g., 10, 50, 100)
  • You need name-to-value mapping at compile time

Thread-Safe Registration

For ThreadSafeFory, register types before spawning threads:

auto fory = Fory::builder().xlang(true).build_thread_safe();

// Register all types first
fory.register_struct<TypeA>(100);
fory.register_struct<TypeB>(101);

// Now safe to use from multiple threads
std::thread t1([&]() {
auto result = fory.serialize(obj_a);
});
std::thread t2([&]() {
auto result = fory.serialize(obj_b);
});

Xlang Registration

For cross-language compatibility, ensure:

  1. Same Type ID: Use identical IDs in all languages
  2. Compatible Types: Use equivalent types across languages

Java

Fory fory = Fory.builder().withXlang(true).build();
fory.register(Person.class, 100);
fory.register(Address.class, 101);

Python

import pyfory

fory = pyfory.Fory(xlang=True)
fory.register(Person, type_id=100)
fory.register(Address, type_id=101)

C++

auto fory = Fory::builder().xlang(true).build();
fory.register_struct<Person>(100);
fory.register_struct<Address>(101);

Built-in Type IDs

Built-in types have pre-assigned type IDs and don't need registration:

Type IDType
0UNKNOWN
1BOOL
2INT8
3INT16
4INT32
5VARINT32
6INT64
7VARINT64
8TAGGED_INT64
9UINT8
10UINT16
11UINT32
12VAR_UINT32
13UINT64
14VAR_UINT64
15TAGGED_UINT64
16FLOAT8
17FLOAT16
18BFLOAT16
19FLOAT32
20FLOAT64
21STRING
22LIST
23SET
24MAP
25ENUM
26NAMED_ENUM
27STRUCT
28COMPATIBLE_STRUCT
29NAMED_STRUCT
30NAMED_COMPATIBLE_STRUCT
31EXT
32NAMED_EXT
33UNION
34TYPED_UNION
35NAMED_UNION
36NONE
37DURATION
38TIMESTAMP
39DATE
40DECIMAL
41BINARY
42ARRAY
43BOOL_ARRAY
44INT8_ARRAY
45INT16_ARRAY
46INT32_ARRAY
47INT64_ARRAY
48UINT8_ARRAY
49UINT16_ARRAY
50UINT32_ARRAY
51UINT64_ARRAY
52FLOAT8_ARRAY
53FLOAT16_ARRAY
54BFLOAT16_ARRAY
55FLOAT32_ARRAY
56FLOAT64_ARRAY
64CHAR
65CHAR16
66CHAR32

Error Handling

Registration errors are checked at serialization/deserialization time:

// Attempting to serialize unregistered type
auto result = fory.serialize(unregistered_obj);
if (!result.ok()) {
// Error: "Type not registered: ..."
std::cerr << result.error().to_string() << std::endl;
}

// Type ID mismatch during deserialization
auto result = fory.deserialize<WrongType>(bytes);
if (!result.ok()) {
// Error: "Type mismatch: expected X, got Y"
std::cerr << result.error().to_string() << std::endl;
}