Composable complex class support for attrs and dataclasses with (un)structuring and validation.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Ready-to-use converters pre-configured for specific serialization formats. Each converter is optimized for its target format with appropriate type handling, encoding settings, and serialization options.
Converter optimized for JSON serialization with support for standard JSON types and common Python data structures.
from cattrs.preconf.json import JsonConverter, make_converter, configure_converter
class JsonConverter(Converter):
"""Converter pre-configured for JSON serialization."""
def __init__(self, **kwargs):
"""Initialize JSON converter with JSON-optimized settings."""
def make_converter(**kwargs) -> JsonConverter:
"""
Factory function to create pre-configured JSON converters.
Parameters:
- **kwargs: Additional arguments passed to JsonConverter
Returns:
JsonConverter instance configured for JSON serialization
"""
def configure_converter(converter: Converter, **kwargs) -> Converter:
"""
Configure any converter for JSON compatibility.
Parameters:
- converter: Converter instance to configure
- **kwargs: Configuration options
Returns:
The configured converter
"""from cattrs.preconf.json import make_converter
import json
converter = make_converter()
# Works seamlessly with json module
data = converter.unstructure(my_object)
json_string = json.dumps(data)
parsed_data = json.loads(json_string)
my_object_copy = converter.structure(parsed_data, MyClass)Converter for YAML serialization with support for YAML-specific features and PyYAML integration.
from cattrs.preconf.pyyaml import PyyamlConverter, make_converter, configure_converter
class PyyamlConverter(Converter):
"""Converter pre-configured for YAML serialization."""
def __init__(self, **kwargs):
"""Initialize YAML converter with YAML-optimized settings."""
def make_converter(**kwargs) -> PyyamlConverter:
"""
Factory function to create pre-configured YAML converters.
Returns:
PyyamlConverter instance configured for YAML serialization
"""
def configure_converter(converter: Converter, **kwargs) -> Converter:
"""
Configure any converter for YAML compatibility.
Parameters:
- converter: Converter instance to configure
Returns:
The configured converter
"""Converter optimized for MessagePack binary serialization format.
from cattrs.preconf.msgpack import MsgpackConverter, make_converter, configure_converter
class MsgpackConverter(Converter):
"""Converter pre-configured for MessagePack serialization."""
def __init__(self, **kwargs):
"""Initialize MessagePack converter."""
def make_converter(**kwargs) -> MsgpackConverter:
"""Factory function to create pre-configured MessagePack converters."""
def configure_converter(converter: Converter, **kwargs) -> Converter:
"""Configure any converter for MessagePack compatibility."""High-performance JSON converter using the orjson library for fast JSON processing.
from cattrs.preconf.orjson import OrjsonConverter, make_converter, configure_converter
class OrjsonConverter(Converter):
"""Converter pre-configured for OrJSON serialization."""
def __init__(self, **kwargs):
"""Initialize OrJSON converter with performance optimizations."""
def make_converter(**kwargs) -> OrjsonConverter:
"""Factory function to create pre-configured OrJSON converters."""
def configure_converter(converter: Converter, **kwargs) -> Converter:
"""Configure any converter for OrJSON compatibility."""JSON converter using ujson for improved JSON processing performance.
from cattrs.preconf.ujson import UjsonConverter, make_converter, configure_converter
class UjsonConverter(Converter):
"""Converter pre-configured for UltraJSON serialization."""
def __init__(self, **kwargs):
"""Initialize UltraJSON converter."""
def make_converter(**kwargs) -> UjsonConverter:
"""Factory function to create pre-configured UltraJSON converters."""
def configure_converter(converter: Converter, **kwargs) -> Converter:
"""Configure any converter for UltraJSON compatibility."""Converter for TOML configuration file format using tomlkit.
from cattrs.preconf.tomlkit import TomlkitConverter, make_converter, configure_converter
class TomlkitConverter(Converter):
"""Converter pre-configured for TOML serialization."""
def __init__(self, **kwargs):
"""Initialize TOML converter with TOML-specific handling."""
def make_converter(**kwargs) -> TomlkitConverter:
"""Factory function to create pre-configured TOML converters."""
def configure_converter(converter: Converter, **kwargs) -> Converter:
"""Configure any converter for TOML compatibility."""Converter for MongoDB's BSON binary format.
from cattrs.preconf.bson import BsonConverter, make_converter, configure_converter
class BsonConverter(Converter):
"""Converter pre-configured for BSON serialization."""
def __init__(self, **kwargs):
"""Initialize BSON converter for MongoDB compatibility."""
def make_converter(**kwargs) -> BsonConverter:
"""Factory function to create pre-configured BSON converters."""
def configure_converter(converter: Converter, **kwargs) -> Converter:
"""Configure any converter for BSON compatibility."""Converter for CBOR (Concise Binary Object Representation) format.
from cattrs.preconf.cbor2 import Cbor2Converter, make_converter, configure_converter
class Cbor2Converter(Converter):
"""Converter pre-configured for CBOR2 serialization."""
def __init__(self, **kwargs):
"""Initialize CBOR2 converter."""
def make_converter(**kwargs) -> Cbor2Converter:
"""Factory function to create pre-configured CBOR2 converters."""
def configure_converter(converter: Converter, **kwargs) -> Converter:
"""Configure any converter for CBOR2 compatibility."""High-performance converter using the msgspec library for JSON and MessagePack serialization.
from cattrs.preconf.msgspec import MsgspecJsonConverter, make_converter, configure_converter
class MsgspecJsonConverter(Converter):
"""Converter pre-configured for msgspec JSON serialization."""
def __init__(self, **kwargs):
"""Initialize msgspec converter with high-performance settings."""
def make_converter(**kwargs) -> MsgspecJsonConverter:
"""Factory function to create pre-configured msgspec converters."""
def configure_converter(converter: Converter, **kwargs) -> Converter:
"""Configure any converter for msgspec compatibility."""from cattrs.preconf.json import make_converter
from attrs import define
@define
class Config:
host: str
port: int
debug: bool = False
# Create JSON-optimized converter
converter = make_converter()
config = Config(host="localhost", port=8080, debug=True)
# Serialize to JSON-compatible format
config_dict = converter.unstructure(config)
# Result: {'host': 'localhost', 'port': 8080, 'debug': True}
# Deserialize from JSON-compatible format
config_copy = converter.structure(config_dict, Config)from cattrs.preconf import json, msgpack, yaml
from attrs import define
from typing import List
@define
class DataModel:
values: List[float]
metadata: dict
data = DataModel(values=[1.1, 2.2, 3.3], metadata={"source": "sensor"})
# Create converters for different formats
json_converter = json.make_converter()
msgpack_converter = msgpack.make_converter()
yaml_converter = yaml.make_converter()
# Same data, different serialization formats
json_data = json_converter.unstructure(data)
msgpack_data = msgpack_converter.unstructure(data)
yaml_data = yaml_converter.unstructure(data)
# All can deserialize back to the same object
data_from_json = json_converter.structure(json_data, DataModel)
data_from_msgpack = msgpack_converter.structure(msgpack_data, DataModel)
data_from_yaml = yaml_converter.structure(yaml_data, DataModel)from cattrs import Converter
from cattrs.preconf.json import configure_converter
# Start with a custom converter
custom_converter = Converter(forbid_extra_keys=True)
# Configure it for JSON compatibility
json_converter = configure_converter(custom_converter)
# Now it has both custom settings and JSON optimizationsEach pre-configured converter requires its corresponding serialization library:
pip install PyYAMLpip install msgpackpip install orjsonpip install ujsonpip install tomlkitpip install pymongopip install cbor2pip install msgspecInstall cattrs with specific format support:
pip install cattrs[orjson,msgpack,pyyaml]Install with Tessl CLI
npx tessl i tessl/pypi-cattrs