Concepts
Extension types allow serialization of objects that don’t have native MessagePack representations. Each extension type has:
-
Type ID - An integer from -128 to 127
-
Packer - Method to convert object to binary
-
Unpacker - Method to reconstruct object from binary
Built-in extension types
Timestamp (-1)
Type ID: -1 (reserved by MessagePack specification)
Serializes Time objects with nanosecond precision.
Format selection
The implementation automatically selects the most compact format:
-
Timestamp32 (4 bytes) - Seconds only, fits in 32 bits
-
Timestamp64 (8 bytes) - Seconds + nanoseconds, fits in 64 bits
-
Timestamp96 (12 bytes) - Full precision
factory.register_type(-1, Time,
packer: Messagepack::Time::Packer,
unpacker: Messagepack::Time::Unpacker
)Custom extension types
Type ID ranges
-
-128 to -1- Reserved for future MessagePack specifications -
0- Convention for symbol extension -
1 to 127- Available for application-specific types
Registration patterns
Method name (simple)
class MyClass
def to_msgpack_ext
# Return binary string
[value].pack("I")
end
def self.from_msgpack_ext(data)
# Parse and return instance
new(data.unpack("I").first)
end
end
factory.register_type(0x10, MyClass,
packer: :to_msgpack_ext,
unpacker: :from_msgpack_ext
)Extension value class
The Messagepack::ExtensionValue class represents raw extension types.
Type ID registry
This table tracks commonly used type IDs:
| Type ID | Type | Status |
|---|---|---|
-128 to -1 | Reserved | MessagePack specification |
0 | Symbol | Convention |
1-127 | Application | Available |
When creating custom extension types: * Check existing registrations for conflicts * Document your type IDs * Consider using a registry for distributed applications