Dataclass Wizard
Dataclass Wizard is a fast, lightweight, and pure Python serialization library for extending native Python dataclasses. It provides elegant tools for marshalling dataclass instances to and from JSON, Python dictionary objects, and environment variables, along with support for field properties with default values. The library is actively maintained, with frequent updates and a recently introduced opt-in v1 engine offering enhanced features and improved performance.
Warnings
- breaking Starting with v1.0.0, the default key transformation for JSON serialization will change from camelCase (e.g., 'myField') to keeping keys as-is (e.g., 'my_field'). Users relying on automatic camelCase conversion should explicitly set `v1_key_case='camel'` in the inner `Meta` class or use `JSONPyWizard` if no transformation is desired.
- deprecated The old 'nested path' functionality (prior to v0.35.0) for mapping deeply nested JSON keys to dataclass fields is deprecated and will no longer be maintained. It has been superseded by enhanced v1 opt-in features.
- gotcha When working with `Union` types that contain nested dataclasses, `dataclass-wizard` may raise `ParseError` if it cannot infer the correct type, especially in ambiguous cases.
- gotcha By default, unknown or extraneous JSON keys encountered during deserialization (`from_dict` or `from_json`) are ignored, and a warning is emitted if debug mode is enabled.
- gotcha Older versions of `dataclass-wizard` (prior to fixes around v0.32.1 and subsequent v1 improvements) might encounter `ParseError` when attempting to parse types that include `typing.Any` when running on Python 3.11+.
- gotcha The `JSONSerializable` (and its alias `JSONWizard` or `DataclassWizard`) mixin class overrides the default `__str__` method to pretty-print the JSON representation of the object, which is useful for debugging but might not be desired for all use cases.
Install
-
pip install dataclass-wizard
Imports
- DataclassWizard
from dataclass_wizard import DataclassWizard
- JSONWizard
from dataclass_wizard import JSONWizard
- EnvWizard
from dataclass_wizard import EnvWizard
- property_wizard
from dataclass_wizard import property_wizard
- json_field
from dataclass_wizard import json_field
Quickstart
import os
from dataclasses import dataclass
from dataclass_wizard import DataclassWizard, EnvWizard
# --- JSON Serialization/Deserialization ---
@dataclass
class User(DataclassWizard):
id: int
name: str
email: str
json_data = '{"id": 1, "name": "Ritvik", "email": "test@example.com"}'
user = User.from_json(json_data)
print(f"Deserialized User: {user!r}")
# Expected: User(id=1, name='Ritvik', email='test@example.com')
user_dict = user.to_dict()
print(f"User to dict: {user_dict}")
# Expected: {'id': 1, 'name': 'Ritvik', 'email': 'test@example.com'}
# --- Environment Variable Loading ---
os.environ['APP_NAME'] = 'MyEnvApp'
os.environ['DEBUG_MODE'] = 'true'
@dataclass
class AppConfig(EnvWizard):
app_name: str
debug_mode: bool
config = AppConfig.from_env()
print(f"App Config: {config!r}")
# Expected: AppConfig(app_name='MyEnvApp', debug_mode=True)