Overview
The@lodum decorator marks a class as lodum-enabled, allowing it to be serialized to and deserialized from various data formats. When applied, it performs eager analysis of the class structure, processes field metadata, and registers the class for type resolution.
Signature
Parameters
The class to decorate. When using
@lodum without parentheses, this is automatically provided. When using @lodum(tag="..."), this is None and the decorator returns a function.Optional tag field name for discriminated unions. When set, this field will be automatically added to serialized output with the value specified in
tag_value. Useful for polymorphic deserialization.The value to use for the tag field. Defaults to the class name if not specified. Only used when
tag is set.Returns
The decorated class with lodum capabilities enabled.Basic Usage
Simple Decoration
With Tagged Unions
Advanced Usage
Nested Classes
With Field Metadata
How It Works
- Registration: The class is registered in the global type registry for forward reference resolution
- Analysis: Class structure, type hints, and field metadata are analyzed
- Init Wrapping: The
__init__method is wrapped to resolveFielddefaults properly - Metadata Attachment: Special attributes are attached:
_lodum_enabled: Set toTrue_lodum_tag: The tag field name (if specified)_lodum_tag_value: The tag value (defaults to class name)_lodum_fields: Dict of field metadata (populated during analysis)
Type Support
The@lodum decorator works with:
- Primitive types:
int,str,float,bool,bytes,None - Collections:
list,dict,set,tuple,frozenset - Standard library:
datetime,date,time,timedelta,UUID,Path,Decimal,Enum - Typing generics:
Optional,Union,List,Dict, etc. - Nested lodum-enabled classes
- Forward references (via string annotations)
Notes
- Classes must have an
__init__method with type-annotated parameters - The decorator performs eager analysis but supports lazy compilation for forward references
- All fields should be instance attributes assigned in
__init__ - The decorator can be used with or without parentheses when no parameters are needed
See Also
- field() - Define field-level metadata
- asdict() - Convert objects to dictionaries
- fromdict() - Create objects from dictionaries
- schema() - Generate JSON Schema