跳到主要内容
版本:dev

Type Registration

This page covers registration APIs for user-defined types.

Why Registration Is Required

User types (struct, class, enum/union, ext types) must be registered before serialization/deserialization.

If a type is missing, deserialization fails with:

  • Type not registered: ...

Register by Numeric ID

Use a stable ID shared by serializer and deserializer peers.

@ForyObject
struct User {
var name: String = ""
var age: Int32 = 0
}

let fory = Fory()
fory.register(User.self, id: 1)

Register by Name

Fully-qualified name

try fory.register(User.self, name: "com.example.User")

name is split by .:

  • namespace: com.example
  • type name: User

Explicit namespace + name

try fory.register(User.self, namespace: "com.example", name: "User")

Consistency Rules

Keep registration mapping consistent across peers:

  • ID mode: same type uses same numeric ID on all peers
  • Name mode: same type uses same namespace and type name on all peers
  • Do not mix ID and name mapping for the same logical type across services

Dynamic Types and Registration

When serializing dynamic values (Any, AnyObject, any Serializer) that contain user-defined types, the concrete runtime types must still be registered.