API Reference
This page contains the technical documentation for all core modules in lodum.
Core API
lodum
lodum(cls=None, tag=None, tag_value=None)
A class decorator that marks a class as lodum-enabled and processes field metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Optional[T]
|
The class to decorate. |
None
|
tag
|
Optional[str]
|
An optional field name to use as a tag for identifying the class in a Union. |
None
|
tag_value
|
Optional[str]
|
An optional value for the tag field. Defaults to the class name. |
None
|
Source code in src/lodum/core.py
asdict(obj)
Recursively converts a lodum-enabled object into plain Python primitives (dict, list, etc.). This handles renaming, skipping fields, and converting enums/datetimes to values.
Source code in src/lodum/__init__.py
fromdict(cls, data)
Hydrates a lodum-enabled class from a dictionary or other plain Python primitives. This performs full type validation and nested object instantiation.
Source code in src/lodum/__init__.py
lodum.core
Context
Holds the serialization/deserialization context, including registry and caches. Encapsulating this allows for isolated serialization environments.
Source code in src/lodum/core.py
Dumper
Bases: Protocol
Defines the interface for a data format dumper (encoder).
Source code in src/lodum/core.py
Loader
Bases: Protocol
Defines the interface for a data format loader (decoder).
Source code in src/lodum/core.py
lodum.field
field(*, rename=None, skip_serializing=False, default=_MISSING, default_factory=None, serializer=None, deserializer=None, validate=None)
Provides metadata to the @lodum decorator for a single field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rename
|
Optional[str]
|
The name to use for the field in the output. |
None
|
skip_serializing
|
bool
|
If |
False
|
default
|
Any
|
A default value to use for the field during decoding if it is missing from the input data. |
_MISSING
|
default_factory
|
Optional[Callable[[], Any]]
|
A zero-argument function that will be called to create a default value for a missing field. |
None
|
serializer
|
Optional[Callable[[Any], Any]]
|
A function to call to encode the field's value. |
None
|
deserializer
|
Optional[Callable[[Any], Any]]
|
A function to call to decode the field's value. |
None
|
validate
|
Optional[Union[Callable[[Any], None], List[Callable[[Any], None]]]]
|
A callable or list of callables to validate the field's value during decoding. |
None
|
Source code in src/lodum/field.py
lodum.concurrency
WorkerThread
Bases: SequentialThread
A faux thread intended to use Web Workers or Node worker_threads.
Note: Full implementation requires a complex JS bridge to bootstrap a new Pyodide environment in the worker. This currently serves as a placeholder that executes sequentially to maintain compatibility.
Source code in src/lodum/concurrency.py
Data Formats
lodum.json
Classes
Functions
dumps(obj)
Encodes a Python object to a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to encode. Must be lodum-enabled or a supported type. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A JSON string representation of the object. |
Source code in src/lodum/json.py
loads(cls, json_string, max_size=DEFAULT_MAX_SIZE)
Decodes a JSON string into a Python object of the specified type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type[T]
|
The class to instantiate. |
required |
json_string
|
str
|
The JSON data to decode. |
required |
max_size
|
int
|
Maximum allowed size of the input string in bytes. |
DEFAULT_MAX_SIZE
|
Returns:
| Type | Description |
|---|---|
T
|
An instance of cls populated with the decoded data. |
Raises:
| Type | Description |
|---|---|
DeserializationError
|
If the input is invalid or exceeds max_size. |
Source code in src/lodum/json.py
lodum.yaml
Classes
YamlDumper
Bases: BaseDumper
Encodes Python objects into a YAML-compatible intermediate representation.
Source code in src/lodum/yaml.py
YamlLoader
Bases: BaseLoader
Decodes a YAML-compatible intermediate representation into Python objects.
Source code in src/lodum/yaml.py
Functions
dumps(obj)
Encodes a Python object to a YAML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to encode. Must be lodum-enabled or a supported type. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A YAML string representation of the object. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If ruamel.yaml is not installed. |
Source code in src/lodum/yaml.py
loads(cls, yaml_string, max_size=DEFAULT_MAX_SIZE)
Decodes a YAML string into a Python object of the specified type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type[T]
|
The class to instantiate. |
required |
yaml_string
|
str
|
The YAML data to decode. |
required |
max_size
|
int
|
Maximum allowed size of the input string in bytes. |
DEFAULT_MAX_SIZE
|
Returns:
| Type | Description |
|---|---|
T
|
An instance of cls populated with the decoded data. |
Raises:
| Type | Description |
|---|---|
DeserializationError
|
If the input is invalid or exceeds max_size. |
ImportError
|
If ruamel.yaml is not installed. |
Source code in src/lodum/yaml.py
lodum.msgpack
Classes
Functions
dumps(obj)
Encodes a Python object to MsgPack bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to encode. Must be lodum-enabled or a supported type. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
The MsgPack-encoded bytes. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If msgpack is not installed. |
Source code in src/lodum/msgpack.py
loads(cls, packed_bytes, max_size=DEFAULT_MAX_SIZE)
Decodes MsgPack bytes into a Python object of the specified type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type[T]
|
The class to instantiate. |
required |
packed_bytes
|
bytes
|
The MsgPack data to decode. |
required |
max_size
|
int
|
Maximum allowed size of the input bytes. |
DEFAULT_MAX_SIZE
|
Returns:
| Type | Description |
|---|---|
T
|
An instance of cls populated with the decoded data. |
Raises:
| Type | Description |
|---|---|
DeserializationError
|
If the input is invalid or exceeds max_size. |
ImportError
|
If msgpack is not installed. |
Source code in src/lodum/msgpack.py
lodum.cbor
Classes
Functions
dumps(obj)
Encodes a Python object to CBOR bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to encode. Must be lodum-enabled or a supported type. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
The CBOR-encoded bytes. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If cbor2 is not installed. |
Source code in src/lodum/cbor.py
loads(cls, cbor_bytes, max_size=DEFAULT_MAX_SIZE)
Decodes CBOR bytes into a Python object of the specified type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type[T]
|
The class to instantiate. |
required |
cbor_bytes
|
bytes
|
The CBOR data to decode. |
required |
max_size
|
int
|
Maximum allowed size of the input bytes. |
DEFAULT_MAX_SIZE
|
Returns:
| Type | Description |
|---|---|
T
|
An instance of cls populated with the decoded data. |
Raises:
| Type | Description |
|---|---|
DeserializationError
|
If the input is invalid or exceeds max_size. |
ImportError
|
If cbor2 is not installed. |
Source code in src/lodum/cbor.py
lodum.bson
Classes
Functions
dumps(obj)
Encodes a Python object to BSON bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to encode. Must be lodum-enabled or a supported type. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
The BSON-encoded bytes. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If bson (pymongo) is not installed. |
Source code in src/lodum/bson.py
loads(cls, bson_bytes, max_size=DEFAULT_MAX_SIZE)
Decodes BSON bytes into a Python object of the specified type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type[T]
|
The class to instantiate. |
required |
bson_bytes
|
bytes
|
The BSON data to decode. |
required |
max_size
|
int
|
Maximum allowed size of the input bytes. |
DEFAULT_MAX_SIZE
|
Returns:
| Type | Description |
|---|---|
T
|
An instance of cls populated with the decoded data. |
Raises:
| Type | Description |
|---|---|
DeserializationError
|
If the input is invalid or exceeds max_size. |
ImportError
|
If bson (pymongo) is not installed. |
Source code in src/lodum/bson.py
lodum.toml
Classes
Functions
dumps(obj)
Encodes a Python object to a TOML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to encode. Must be lodum-enabled or a supported type. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A TOML string representation of the object. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If tomli-w is not installed. |
Source code in src/lodum/toml.py
loads(cls, toml_string, max_size=DEFAULT_MAX_SIZE)
Decodes a TOML string into a Python object of the specified type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type[T]
|
The class to instantiate. |
required |
toml_string
|
str
|
The TOML data to decode. |
required |
max_size
|
int
|
Maximum allowed size of the input string in bytes. |
DEFAULT_MAX_SIZE
|
Returns:
| Type | Description |
|---|---|
T
|
An instance of cls populated with the decoded data. |
Raises:
| Type | Description |
|---|---|
DeserializationError
|
If the input is invalid or exceeds max_size. |
ImportError
|
If tomllib (or tomli) is not installed. |
Source code in src/lodum/toml.py
lodum.pickle
Classes
SafeUnpickler
Bases: Unpickler
A custom unpickler that only allows safe, lodum-enabled classes to be loaded.
Source code in src/lodum/pickle.py
ValidationDumper
Bases: Dumper
A no-op dumper used only for validation.
Source code in src/lodum/pickle.py
Functions
dumps(obj)
Encodes a Python object to a pickle byte string, ensuring it is safe.
loads(cls, data, max_size=DEFAULT_MAX_SIZE)
Decodes a pickle byte string to a Python object, ensuring it is safe.