Migrating Between Lodum Versions
Exception Standardization (v0.2.0)
In versions prior to v0.2.0, the YAML and Pickle loaders raisedTypeError when a field’s type did not match the expected type during deserialization. This has been standardized to lodum.exception.DeserializationError to match the behavior of other formats like JSON and MsgPack.
Before (v0.1.x):
Key Differences from Other Libraries
Lodum is inspired by Rust’sserde framework. Its primary differences from other Python libraries are:
- Format Agnostic: Lodum separates the definition of your data structure (using
@lodum) from the data format (JSON, YAML, TOML, MsgPack, etc.). - Bytecode Compilation: Lodum generates specialized Python bytecode for your classes at runtime. This provides performance comparable to hand-written code while remaining pure Python.
__init__-Centric: Lodum uses your class’s__init__method and its type hints as the source of truth for the data structure. This ensures that your objects are always instantiated through their standard constructor.
Migrating from Pydantic
Pydantic is a popular library that usesBaseModel and class attributes to define data structures.
Class Definition
Pydantic:You can also use
@dataclass with @lodum for a more concise syntax:Serialization
Pydantic:Deserialization
Pydantic:Field Customization
Pydantic:Validation
Pydantic:Migrating from Marshmallow
Marshmallow uses separateSchema classes to define how data is serialized and deserialized.
Definition and Usage
Marshmallow:@post_load boilerplate.
Nested Objects
Marshmallow:Migrating from Dataclasses + Mashumaro/Dacite
If you are already usingdataclasses with a library like mashumaro, the transition to Lodum is very smooth.
Mashumaro:
mashumaro through bytecode generation, but provides a more unified interface for multiple binary and text formats out of the box.
Multiple Format Support
One key advantage of Lodum: Mashumaro (requires different mixins):Migrating from Attrs + Cattrs
attrs is an alternative to dataclasses, and cattrs handles the conversion to/from structured data.
Attrs/Cattrs:
cattrs is very flexible, Lodum provides a more integrated experience with direct support for various wire formats.
Custom Converters
Cattrs:Migrating from JSON/Pickle Standard Library
If you’re currently using Python’s built-injson or pickle modules directly:
Standard Library:
Performance Comparison
See the Performance Benchmarks page for detailed comparisons. Summary:- Lodum vs Marshmallow: 2x faster on average
- Lodum vs Pydantic v2: Pydantic is faster due to Rust core, but Lodum is competitive in pure Python
- Lodum vs Standard Library: Similar performance for simple objects, better for complex nested structures
Feature Comparison
| Feature | Lodum | Pydantic | Marshmallow | Cattrs |
|---|---|---|---|---|
| Multiple formats | ✅ | ❌ | ❌ | ✅ |
| Type validation | ✅ | ✅ | ✅ | ✅ |
| Custom validators | ✅ | ✅ | ✅ | ✅ |
| Field renaming | ✅ | ✅ | ✅ | ✅ |
| Streaming support | ✅ | ❌ | ❌ | ❌ |
| Schema generation | ✅ | ✅ | ✅ | ❌ |
| Pure Python | ✅ | ❌ | ✅ | ✅ |
| WASM support | ✅ | ❌ | ✅ | ✅ |
| Dataclass support | ✅ | ✅ | ✅ | ✅ |
Common Migration Patterns
Pattern 1: Adding Type Hints
Many older codebases don’t have type hints. You’ll need to add them:Pattern 2: Replacing Custom Serialization Logic
Pattern 3: Gradual Migration
You can migrate incrementally:Getting Help
If you encounter issues during migration:- Check the API Reference for detailed documentation
- Look at the Guides for common patterns
- Open an issue on GitHub with your migration question