Type Registration
This page covers Python type registration APIs. Use Configuration for strict-mode policy, max-depth limits, and trusted-data guidance.
Type Registration
Register xlang classes by type name so other runtimes can resolve the same schema identity:
from dataclasses import dataclass
import pyfory
fory = pyfory.Fory(xlang=True, strict=True)
@dataclass
class User:
name: str
age: pyfory.Int32
fory.register(User, typename="example.User")
For Python native mode, numeric type IDs are the compact same-language registration path:
import pyfory
fory = pyfory.Fory(xlang=False, strict=True)
fory.register(MyClass, type_id=100)
Registration Patterns
Use the registration form that matches the payload contract:
# Xlang: stable namespace/type-name identity
fory.register(MyClass, typename="com.example.MyClass")
# Native mode: compact numeric identity
fory.register(MyClass, type_id=100)
# Custom serializer
fory.register(MyClass, type_id=100, serializer=MySerializer(fory.type_resolver, MyClass))
# Batch registration
type_id = 100
for model_class in [User, Order, Product, Invoice]:
fory.register(model_class, type_id=type_id)
type_id += 1
Strict Mode Relationship
With strict=True, deserialization accepts only registered types. Register all
application classes before serializing or deserializing payloads, and keep the
same registration IDs or names on every peer that shares those payloads.
Related Topics
- Configuration - Fory parameters
- Configuration - Strict mode, deserialization policies, and size limits
- Custom Serializers - Custom serialization