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. Field metadata is processed lazily during first serialization/deserialization to correctly handle forward references and circular dependencies.
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
begin_list()
end_list()
field(name, value, handler, depth=0, seen=None)
list_item(value, handler, depth=0, seen=None)
StreamingDumper
Bases: Dumper
Base class for dumpers that write directly to an IO target.
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
|
str | None
|
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
|
Callable[[], Any] | None
|
A zero-argument function that will be called to create a default value for a missing field. |
None
|
serializer
|
Callable[[Any], Any] | None
|
A function to call to encode the field's value. |
None
|
deserializer
|
Callable[[Any], Any] | None
|
A function to call to decode the field's value. |
None
|
validate
|
Callable[[Any], None] | list[Callable[[Any], None]] | 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
JsonStreamingDumper
Bases: StreamingDumper
Writes JSON tokens directly to a stream.
Source code in src/lodum/json.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
Functions:
dump(obj, target=None, **kwargs)
Encodes a Python object to JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to encode. |
required |
target
|
IO[str] | Path | None
|
Optional file-like object or Path to write to. |
None
|
**kwargs
|
Any
|
Additional arguments for json.dump(s) (e.g., indent). Note: If target is provided, O(1) streaming is used and some formatting kwargs might be ignored in the current implementation. |
{}
|
Returns:
| Type | Description |
|---|---|
str | None
|
The JSON string if target is None, otherwise None. |
Source code in src/lodum/json.py
dumps(obj, **kwargs)
load(cls, source, max_size=DEFAULT_MAX_SIZE)
Decodes JSON from a string, stream, or file into a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
The class to instantiate. |
required |
source
|
str | IO[Any] | Path
|
JSON string, file-like object, or Path. |
required |
max_size
|
int
|
Maximum allowed size for string input. |
DEFAULT_MAX_SIZE
|
Returns:
| Type | Description |
|---|---|
T
|
An instance of cls. |
Source code in src/lodum/json.py
load_stream(cls, stream_io)
loads(cls, json_string, **kwargs)
schema(cls)
stream(cls, source)
Lazily decodes a stream of JSON objects into instances of cls.
Intended for sources containing a top-level array of objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
The class to instantiate for each item. |
required |
source
|
IO[bytes] | Path
|
A binary stream, file-like object, or Path to a JSON array. |
required |
Returns:
| Type | Description |
|---|---|
Iterator[T]
|
An iterator yielding instances of |
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:
dump(obj, target=None, **kwargs)
Encodes a Python object to YAML.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to encode. |
required |
target
|
IO[str] | Path | None
|
Optional file-like object or Path to write to. |
None
|
**kwargs
|
Any
|
Additional arguments for yaml.dump. |
{}
|
Returns:
| Type | Description |
|---|---|
str | None
|
The YAML string if target is None, otherwise None. |
Source code in src/lodum/yaml.py
dumps(obj, **kwargs)
load(cls, source, max_size=DEFAULT_MAX_SIZE)
Decodes YAML from a string, stream, or file into a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
The class to instantiate. |
required |
source
|
str | IO[Any] | Path
|
YAML string, file-like object, or Path. |
required |
max_size
|
int
|
Maximum allowed size for string input. |
DEFAULT_MAX_SIZE
|
Returns:
| Type | Description |
|---|---|
T
|
An instance of cls. |
Source code in src/lodum/yaml.py
loads(cls, yaml_string, **kwargs)
schema(cls)
stream(cls, source)
Lazily decodes a stream of YAML documents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
The class to instantiate for each item. |
required |
source
|
IO[Any] | Path
|
A stream, file-like object, or Path. |
required |
Returns:
| Type | Description |
|---|---|
Iterator[T]
|
An iterator yielding instances of |
Source code in src/lodum/yaml.py
lodum.msgpack
Classes
Functions:
dump(obj, target=None, **kwargs)
Encodes a Python object to MsgPack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to encode. |
required |
target
|
IO[bytes] | Path | None
|
Optional file-like object or Path to write to. |
None
|
**kwargs
|
Any
|
Additional arguments for msgpack.packb. |
{}
|
Returns:
| Type | Description |
|---|---|
bytes | None
|
The MsgPack bytes if target is None, otherwise None. |
Source code in src/lodum/msgpack.py
dumps(obj, **kwargs)
load(cls, source, max_size=DEFAULT_MAX_SIZE)
Decodes MsgPack from bytes, stream, or file into a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
The class to instantiate. |
required |
source
|
bytes | IO[bytes] | Path
|
MsgPack bytes, file-like object, or Path. |
required |
max_size
|
int
|
Maximum allowed size for bytes input. |
DEFAULT_MAX_SIZE
|
Returns:
| Type | Description |
|---|---|
T
|
An instance of cls. |
Source code in src/lodum/msgpack.py
loads(cls, packed_bytes, **kwargs)
stream(cls, source)
Lazily decodes a stream of MsgPack objects. Supports concatenated MsgPack objects (ND-MsgPack style).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
The class to instantiate for each item. |
required |
source
|
IO[bytes] | Path
|
A binary stream, file-like object, or Path. |
required |
Returns:
| Type | Description |
|---|---|
Iterator[T]
|
An iterator yielding instances of |
Source code in src/lodum/msgpack.py
lodum.cbor
Classes
Functions:
dump(obj, target=None, **kwargs)
Encodes a Python object to CBOR.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to encode. |
required |
target
|
IO[bytes] | Path | None
|
Optional file-like object or Path to write to. |
None
|
**kwargs
|
Any
|
Additional arguments for cbor2.dump(s). |
{}
|
Returns:
| Type | Description |
|---|---|
bytes | None
|
The CBOR bytes if target is None, otherwise None. |
Source code in src/lodum/cbor.py
dumps(obj, **kwargs)
load(cls, source, max_size=DEFAULT_MAX_SIZE)
Decodes CBOR from bytes, stream, or file into a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
The class to instantiate. |
required |
source
|
bytes | IO[bytes] | Path
|
CBOR bytes, file-like object, or Path. |
required |
max_size
|
int
|
Maximum allowed size for bytes input. |
DEFAULT_MAX_SIZE
|
Returns:
| Type | Description |
|---|---|
T
|
An instance of cls. |
Source code in src/lodum/cbor.py
loads(cls, cbor_bytes, **kwargs)
stream(cls, source)
Lazily decodes a stream of CBOR objects. Supports concatenated CBOR objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
The class to instantiate for each item. |
required |
source
|
IO[bytes] | Path
|
A binary stream, file-like object, or Path. |
required |
Returns:
| Type | Description |
|---|---|
Iterator[T]
|
An iterator yielding instances of |
Source code in src/lodum/cbor.py
lodum.bson
Classes
Functions:
dump(obj, target=None, **kwargs)
Encodes a Python object to BSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to encode. |
required |
target
|
IO[bytes] | Path | None
|
Optional file-like object or Path to write to. |
None
|
**kwargs
|
Any
|
Additional arguments for bson.encode. |
{}
|
Returns:
| Type | Description |
|---|---|
bytes | None
|
The BSON bytes if target is None, otherwise None. |
Source code in src/lodum/bson.py
dumps(obj, **kwargs)
load(cls, source, max_size=DEFAULT_MAX_SIZE)
Decodes BSON from bytes, stream, or file into a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
The class to instantiate. |
required |
source
|
bytes | IO[bytes] | Path
|
BSON bytes, file-like object, or Path. |
required |
max_size
|
int
|
Maximum allowed size for bytes input. |
DEFAULT_MAX_SIZE
|
Returns:
| Type | Description |
|---|---|
T
|
An instance of cls. |
Source code in src/lodum/bson.py
loads(cls, bson_bytes, **kwargs)
stream(cls, source)
Lazily decodes a stream of BSON objects. Supports concatenated BSON objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
The class to instantiate for each item. |
required |
source
|
IO[bytes] | Path
|
A binary stream, file-like object, or Path. |
required |
Returns:
| Type | Description |
|---|---|
Iterator[T]
|
An iterator yielding instances of |
Source code in src/lodum/bson.py
lodum.toml
Classes
Functions:
dump(obj, target=None, **kwargs)
Encodes a Python object to TOML.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to encode. |
required |
target
|
IO[str] | Path | None
|
Optional file-like object or Path to write to. |
None
|
**kwargs
|
Any
|
Additional arguments for tomli_w.dump(s). |
{}
|
Returns:
| Type | Description |
|---|---|
str | None
|
The TOML string if target is None, otherwise None. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If tomli-w is not installed. |
Source code in src/lodum/toml.py
dumps(obj, **kwargs)
load(cls, source, max_size=DEFAULT_MAX_SIZE)
Decodes TOML from a string, stream, or file into a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
The class to instantiate. |
required |
source
|
str | IO[Any] | Path
|
TOML string, file-like object, or Path. |
required |
max_size
|
int
|
Maximum allowed size for string input. |
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
loads(cls, toml_string, **kwargs)
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:
dump(obj, target=None, **kwargs)
Encodes a Python object to a pickle byte string, ensuring it is safe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to encode. |
required |
target
|
IO[bytes] | Path | None
|
Optional file-like object or Path to write to. |
None
|
**kwargs
|
Any
|
Additional arguments for pickle.dump(s). |
{}
|
Returns:
| Type | Description |
|---|---|
bytes | None
|
The pickle bytes if target is None, otherwise None. |
Source code in src/lodum/pickle.py
dumps(obj, **kwargs)
load(cls, source, max_size=DEFAULT_MAX_SIZE)
Decodes a pickle from bytes, stream, or file into a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
The class to instantiate. |
required |
source
|
bytes | IO[bytes] | Path
|
pickle bytes, file-like object, or Path. |
required |
max_size
|
int
|
Maximum allowed size for bytes input. |
DEFAULT_MAX_SIZE
|
Returns:
| Type | Description |
|---|---|
T
|
An instance of cls. |