Serialization¶
casty.TypeRegistry
¶
Bidirectional mapping between fully-qualified type names and Python classes.
Supports both explicit registration and auto-resolution via
importlib.
Examples:
>>> from dataclasses import dataclass
>>> @dataclass(frozen=True)
... class Ping:
... value: int
>>> registry = TypeRegistry()
>>> registry.register(Ping)
>>> registry.resolve(registry.type_name(Ping)) is Ping
True
__init__()
¶
register(cls)
¶
Register a type for serialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The class to register. |
required |
register_all(*types)
¶
resolve(name)
¶
Resolve a fully-qualified type name to a Python class.
Falls back to auto-resolution via importlib if the name is
not explicitly registered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Fully-qualified type name (e.g. |
required |
Returns:
| Type | Description |
|---|---|
type
|
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If the type cannot be resolved. |
type_name(cls)
¶
Return the fully-qualified name for a type, registering it if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The class to look up. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Fully-qualified name (e.g. |
is_registered(cls)
¶
casty.JsonSerializer
¶
JSON-based serializer with recursive handling of Casty types.
Handles ActorRef, frozenset, Enum, dict, tuple,
and frozen dataclasses registered in the TypeRegistry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
TypeRegistry
|
Registry for resolving type names. |
required |
Examples:
>>> registry = TypeRegistry()
>>> ser = JsonSerializer(registry)
>>> ser.deserialize(ser.serialize({"key": "value"}))
{'key': 'value'}
registry
property
¶
Return the underlying type registry.
__init__(registry)
¶
serialize(obj)
¶
Serialize an object to JSON bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
M
|
Object to serialize. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
UTF-8 encoded JSON. |
deserialize(data, *, ref_factory=None)
¶
Deserialize JSON bytes back to a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
UTF-8 encoded JSON from |
required |
ref_factory
|
Callable or None
|
Factory for reconstructing |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
|
to_dict(value)
¶
from_dict(value, *, ref_factory=None)
¶
casty.PickleSerializer
¶
Pickle-based serializer using protocol 5.
Faster than JSON but not human-readable. Suitable for trusted environments where all nodes run the same code.
Examples:
serialize(obj)
¶
Serialize an object to pickle bytes (protocol 5).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
M
|
Object to serialize. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
|
deserialize(data, *, ref_factory=None)
¶
Deserialize pickle bytes back to a Python object.
Temporarily installs ref_factory as the module-level restore
hook for the duration of pickle.loads(), then restores the
previous value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Bytes produced by |
required |
ref_factory
|
Callable or None
|
Factory for reconstructing |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
|
casty.CompressedSerializer
¶
Composable wrapper that adds compression to any Serializer.
Applies compression on serialize() and decompression on
deserialize(), delegating the actual serialization to the
wrapped inner serializer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inner
|
Serializer
|
The underlying serializer to wrap. |
required |
config
|
CompressionConfig
|
Compression algorithm and level settings. |
required |
Examples:
>>> from casty.config import CompressionConfig
>>> ser = CompressedSerializer(PickleSerializer(), CompressionConfig(level=9))
>>> ser.deserialize(ser.serialize({"key": "value"}))
{'key': 'value'}
__init__(inner, config)
¶
serialize(obj)
¶
Serialize and compress an object to bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
M
|
Object to serialize. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
Compressed serialized bytes. |
deserialize(data, *, ref_factory=None)
¶
Decompress and deserialize bytes back to an object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Compressed bytes from |
required |
ref_factory
|
Callable or None
|
Factory for reconstructing |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
|
casty.Serializer
¶
Bases: Protocol
Protocol for message serialization and deserialization.
Implementations must handle ActorRef round-tripping via the
ref_factory keyword argument passed to deserialize().
Examples:
Minimal implementation:
casty.build_serializer(config, *, registry=None)
¶
Build a Serializer chain from configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
SerializationConfig
|
Serialization settings (serializer kind + optional compression). |
required |
registry
|
TypeRegistry | None
|
Type registry required when |
None
|
Returns:
| Type | Description |
|---|---|
PickleSerializer | JsonSerializer | CompressedSerializer
|
Ready-to-use serializer, possibly wrapped with compression. |
Examples:
casty.MsgpackSerializer
¶
MessagePack serializer with structured type handling.
Reuses the same recursive dict-conversion as JsonSerializer but
encodes to MessagePack binary format, giving compact output with
fast encode/decode.
Requires the optional msgpack package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
TypeRegistry
|
Registry for resolving type names. |
None
|
Examples:
>>> registry = TypeRegistry()
>>> ser = MsgpackSerializer(registry)
>>> ser.deserialize(ser.serialize({"key": "value"}))
{'key': 'value'}
registry
property
¶
Return the underlying type registry.
__init__(registry=None)
¶
serialize(obj)
¶
Serialize an object to MessagePack bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
M
|
Object to serialize. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
|
deserialize(data, *, ref_factory=None)
¶
Deserialize MessagePack bytes back to a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Bytes from |
required |
ref_factory
|
Callable or None
|
Factory for reconstructing |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
|
casty.CloudPickleSerializer
¶
CloudPickle-based serializer for lambdas and closures.
Drop-in replacement for PickleSerializer that handles lambdas,
closures, and dynamically defined classes via cloudpickle.
Deserialization uses standard pickle.loads.
Requires the optional cloudpickle package.
Examples:
>>> ser = CloudPickleSerializer()
>>> ser.deserialize(ser.serialize({"key": "value"}))
{'key': 'value'}
serialize(obj)
¶
Serialize an object using cloudpickle (protocol 5).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
M
|
Object to serialize. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
|
deserialize(data, *, ref_factory=None)
¶
Deserialize bytes back to a Python object.
Uses standard pickle.loads since cloudpickle only customizes
the dump side.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Bytes from |
required |
ref_factory
|
Callable or None
|
Factory for reconstructing |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
|
casty.Lz4CompressedSerializer
¶
Wraps any serializer with LZ4 frame compression.
Applies LZ4 compression on serialize() and decompression on
deserialize(), delegating actual serialization to the wrapped
inner serializer.
Requires the optional lz4 package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inner
|
Serializer
|
The underlying serializer to wrap. |
required |
level
|
int
|
LZ4 compression level (0 = default, higher = more compression). |
0
|
Examples:
>>> from casty.remote.serialization import PickleSerializer
>>> ser = Lz4CompressedSerializer(PickleSerializer())
>>> ser.deserialize(ser.serialize({"key": "value"}))
{'key': 'value'}
__init__(inner, *, level=0)
¶
serialize(obj)
¶
Serialize and compress an object to bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
M
|
Object to serialize. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
LZ4-compressed serialized bytes. |
deserialize(data, *, ref_factory=None)
¶
Decompress and deserialize bytes back to an object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
LZ4-compressed bytes from |
required |
ref_factory
|
Callable or None
|
Factory for reconstructing |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
|