@lodum decorator is the core annotation that enables your Python classes for serialization and deserialization across multiple formats.
Basic Usage
Apply the decorator to any class to make it serializable:- Regular classes with
__init__methods @dataclassclasses- Classes with type hints (required)
Type hints are required for all fields. The
@lodum decorator uses them to understand your data structure and generate optimized serialization code.How It Works
When you apply@lodum to a class, the decorator:
- Registers the class in the global type registry for forward reference resolution
- Wraps
__init__to resolveFielddefaults automatically - Analyzes the class structure eagerly to extract field metadata and type information
- Generates optimized bytecode using an AST compiler for ~64% faster serialization
src/lodum/core.py:82-131:
Tagged Unions
Use thetag parameter to enable discriminated union support for polymorphic types:
Custom Tag Values
Override the tag value withtag_value:
Tagged unions solve the ambiguity problem when deserializing Union types with similar structures. Without tags, lodum tries each type in order and uses the first that succeeds.
Decorator Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
cls | Type[T] | None | The class being decorated (auto-filled) |
tag | str | None | None | Field name for the discriminator tag |
tag_value | str | None | cls.__name__ | Value to use for the discriminator |
Type Signature
With Dataclasses
The@lodum decorator composes well with @dataclass:
Performance Benefits
The decorator generates specialized bytecode during class analysis:- ~64% faster dumps compared to generic introspection
- ~35% faster loads compared to baseline deserialization
- O(1) compilation overhead - happens once per class
Thread Safety
Class registration and analysis are thread-safe. The decorator uses internal locks to ensure:- Multiple threads can safely decorate classes concurrently
- The global type registry remains consistent
- Bytecode compilation happens exactly once per class
Related
- Field Customization - Customize individual field behavior
- Format Support - Use your lodum classes with different formats
- Validation System - Add validation rules to fields