Simple Python dataclass serialization package with no dependencies
Project description
StupidSimple Dataclasses Codec
This native Python package allows to easily convert dataclasses into and from a serialized form. By default supports json.
Installation
pip install dataclasses-codec
Usage
The package provides functions to easily convert dataclasses into and from a serialized form. It uses by default the json_codec instance the package provides.
from dataclasses import dataclass
from dataclasses_codec import encode, decode, to_json, from_json
@dataclass
class MyDataclass:
first_name: str
last_name: str
obj = MyDataclass("John", "Doe")
# Serializable Python dictionary
encoded = encode(obj)
print(encoded)
# Output: {'first_name': 'John', 'last_name': 'Doe'}
decoded = decode(MyDataclass, encoded)
print(decoded)
# Output: MyDataclass(first_name="John", last_name="Doe")
# Native JSON handling
raw_json = to_json(obj)
print(raw_json)
# Output: '{"first_name": "John", "last_name": "Doe"}'
restored = from_json(MyDataclass, raw_json)
print(restored)
# Output: MyDataclass(first_name="John", last_name="Doe")
JSON codec
The JSON codec is a first class citizen of the package. It allows to easily convert dataclasses into and from JSON strings.
Serialization can be customized by using the json_field value. It supports native conversion of date and datetime fields. Dataclasses can be nested to form complex objects.
json_field extends the native dataclass field decorator to support custom serialization and deserialization.
from dataclasses import dataclass
from dataclasses_codec import json_codec, JSONOptional, JSON_MISSING
from dataclasses_codec.codecs.json import json_field
import datetime as dt
# Still a dataclass, so we can use its features like slots, frozen, etc.
@dataclass(slots=True)
class MyMetadataDataclass:
created_at: dt.datetime
updated_at: dt.datetime = json_field(
serializer=lambda d: d.isoformat(),
deserializer=lambda s: dt.datetime.fromisoformat(s)
)
enabled: bool | JSONOptional = JSON_MISSING # Explicitly mark a field as optional
description: str | None = None # None is intentionally serialized as null
@dataclass
class MyDataclass:
first_name: str
last_name: str
age: int
metadata: MyMetadataDataclass = json_field(
json_name="meta"
)
obj = MyDataclass("John", "Doe", 30, MyMetadataDataclass(dt.datetime.now(), dt.datetime.now()))
raw_json = json_codec.to_json(obj)
print(raw_json)
# Output: '{"first_name": "John", "last_name": "Doe", "age": 30, "meta": {"created_at": "2025-10-25T11:53:35.918899", "updated_at": "2025-10-25T11:53:35.918902", "description": null}}'
JSON Mixins
The package provides JSON mixins to add the serialization and deserialization capabilities to a dataclass.
By default, the JSON codec is used. See: dataclasses_codec.codecs.json for more details.
from dataclasses import dataclass
from dataclasses_codec import JSONSerializable, JSONDeserializable
@dataclass
class MyDataclass(JSONSerializable, JSONDeserializable):
first_name: str
last_name: str
age: int
obj = MyDataclass("John", "Doe", 30)
encoded = obj.to_dict(to_camel_case=True)
print(encoded)
# Output: {'firstName': 'John', 'lastName': 'Doe', 'age': 30}
restored = MyDataclass.from_dict(encoded, to_snake_case=True)
print(restored)
# Output: MyDataclass(first_name="John", last_name="Doe", age=30)
Extensibility
The package allows to extend the functionality of the codecs by implementing the Codec interface.
Each implementation should use is_dataclass to check if the object is a dataclass.
from dataclasses import dataclass, is_dataclass
from dataclasses_codec import Codec, encode
from dataclasses_codec.errors import CodecError
class MyCodec(Codec):
def encode(self, obj, **kwargs):
if not is_dataclass(obj):
raise CodecError(f"obj is not a dataclass, found: '{obj}'")
return obj
def decode(self, cls, data, **kwargs):
# Implement the decoding logic here
raise CodecError("Decoding is not implemented for this codec")
@dataclass
class MyDataclass:
first_name: str
last_name: str
age: int
obj = MyDataclass("John", "Doe", 30)
encoded = encode(obj, codec=MyCodec())
print(encoded)
# Output: {'first_name': 'John', 'last_name': 'Doe', 'age': 30}
Development
Running the tests
python -m unittest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dataclasses_codec-0.1.0.tar.gz.
File metadata
- Download URL: dataclasses_codec-0.1.0.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
318ed3d8c418075dc033ba4bcea4eae7ca546004705ff961c5f1a3399cad1332
|
|
| MD5 |
a9a37c1c072112074928aa56a08c10bd
|
|
| BLAKE2b-256 |
b214b60ab8d303805d4891f82f0d6ccde91fcc94ce3ba5a27247fd264e6694ff
|
Provenance
The following attestation bundles were made for dataclasses_codec-0.1.0.tar.gz:
Publisher:
publish.yml on stupid-simple/dataclasses-codec
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dataclasses_codec-0.1.0.tar.gz -
Subject digest:
318ed3d8c418075dc033ba4bcea4eae7ca546004705ff961c5f1a3399cad1332 - Sigstore transparency entry: 640146183
- Sigstore integration time:
-
Permalink:
stupid-simple/dataclasses-codec@98bd39dd57d9d6aefa0e12f2a12ab165603f5d99 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/stupid-simple
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@98bd39dd57d9d6aefa0e12f2a12ab165603f5d99 -
Trigger Event:
push
-
Statement type:
File details
Details for the file dataclasses_codec-0.1.0-py3-none-any.whl.
File metadata
- Download URL: dataclasses_codec-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79e891c34f5a10f643a8297e34d61d254677a0d8c3a99f0a6ac500a7178618d5
|
|
| MD5 |
f949e3278221486d15d79a22c63da8f6
|
|
| BLAKE2b-256 |
a1857ff862984abf7434e30a93e9630f3eec12910d75f373d0f7b6c448266f7b
|
Provenance
The following attestation bundles were made for dataclasses_codec-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on stupid-simple/dataclasses-codec
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dataclasses_codec-0.1.0-py3-none-any.whl -
Subject digest:
79e891c34f5a10f643a8297e34d61d254677a0d8c3a99f0a6ac500a7178618d5 - Sigstore transparency entry: 640146228
- Sigstore integration time:
-
Permalink:
stupid-simple/dataclasses-codec@98bd39dd57d9d6aefa0e12f2a12ab165603f5d99 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/stupid-simple
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@98bd39dd57d9d6aefa0e12f2a12ab165603f5d99 -
Trigger Event:
push
-
Statement type: