Paper-thin JSON serialization/deserialization for Python dataclasses
Project description
paperjson
Paper-thin JSON serialization/deserialization for Python dataclasses. Easy to extend for custom classes.
Usage
There are two ways to add to_json() / from_json() to a dataclass:
1A. Inherit from SerdesBase
Inheriting from SerdesBase gives full type-checker / LSP support — your
editor will know about to_json() and from_json():
from dataclasses import dataclass
import paperjson
@dataclass
class User(paperjson.SerdesBase):
name: str
email: str
user = User(name="Alice", email="alice@example.com")
print(user.to_json()) # {"name": "Alice", "email": "alice@example.com"}
restored = User.from_json(user.to_json())
print(restored == user) # True
This is similar to Pydantic BaseModel. But! You might not wish to add a base to your class.
1B. Use the @serdes decorator
Decorate any dataclass to inject the methods at runtime. It works identically, but type checkers can't see the injected methods:
from dataclasses import dataclass
import paperjson
@paperjson.serdes
@dataclass
class User:
name: str
email: str
user = User(name="Alice", email="alice@example.com")
print(user.to_json()) # {"name": "Alice", "email": "alice@example.com"}
You may choose both without conflict — inherit from SerdesBase and decorate with @serdes.
2. Type annotations with SerdesProtocol
Use SerdesProtocol in function signatures to accept anything that has
to_json() / from_json() — whether it inherits from SerdesBase or was
decorated:
from typing import Any
import paperjson
def dump(obj: paperjson.SerdesProtocol[Any]) -> str:
return obj.to_json(indent=2)
def load(cls: type[SerdesProtocol[Any]], data: str) -> Any:
return cls.from_json(data)
3. Custom type support
from decimal import Decimal
import paperjson
@paperjson.register_serializer(Decimal)
def _(val: Decimal) -> str:
return str(val)
@paperjson.register_deserializer(Decimal)
def _(val: str) -> Decimal:
return Decimal(val)
4. Worked example
from datetime import datetime, timezone
from pathlib import Path
from dataclasses import dataclass
import paperjson
@dataclass
class Address(paperjson.SerdesBase):
line1: str
line2: str
city: str
st: str
zip: str
@paperjson.serdes
@dataclass
class User():
name: str
dob: datetime
email: str
homedir: Path
mail: Address
obj = User(
name="Alice",
dob=datetime.now(timezone.utc),
email="alice@example.com",
homedir=Path.home(),
mail=Address("123 Main St", "", "Springfield", "IL", "62701"),
)
json_str = obj.to_json()
restored = User.from_json(json_str)
Output dataclasses are identical to those created:
>>> print(restored)
User(name='Alice', dob=datetime.datetime(2026, 5, 19, 4, 50, 7, 485835, tzinfo=datetime.timezone.utc), email='alice@example.com', homedir=PosixPath('/home/tim'), mail=Address(line1='123 Main St', line2='', city='Springfield', st='IL', zip='62701'))
>>> obj == restored
True
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 paperjson-0.1.0.tar.gz.
File metadata
- Download URL: paperjson-0.1.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb6be0c051bcace08b5bee97bfc647b54e97e1ae19b94fbe22a93a7c1fdb217f
|
|
| MD5 |
5d4dd71b203099bc63e5e25d336b3bad
|
|
| BLAKE2b-256 |
84ac9dc55c68c7f3c87ee77f71ac4503196468d4d75b2818ea3b164d29be169b
|
Provenance
The following attestation bundles were made for paperjson-0.1.0.tar.gz:
Publisher:
publish.yml on timrburnham/paperjson
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
paperjson-0.1.0.tar.gz -
Subject digest:
cb6be0c051bcace08b5bee97bfc647b54e97e1ae19b94fbe22a93a7c1fdb217f - Sigstore transparency entry: 1572126391
- Sigstore integration time:
-
Permalink:
timrburnham/paperjson@c917aae19f378ed0d9172d3835b0e742d2a4bbf8 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/timrburnham
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c917aae19f378ed0d9172d3835b0e742d2a4bbf8 -
Trigger Event:
release
-
Statement type:
File details
Details for the file paperjson-0.1.0-py3-none-any.whl.
File metadata
- Download URL: paperjson-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca8684f00ceced9ddda3cf05a0698a0385951b0abd0e1009e1b8d440d0beb93c
|
|
| MD5 |
a6277303ee250da84716de4b46c84a33
|
|
| BLAKE2b-256 |
0f3bd3d68828d9b11a300a223e00e6215e3bc249966937efbbc486170da5f462
|
Provenance
The following attestation bundles were made for paperjson-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on timrburnham/paperjson
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
paperjson-0.1.0-py3-none-any.whl -
Subject digest:
ca8684f00ceced9ddda3cf05a0698a0385951b0abd0e1009e1b8d440d0beb93c - Sigstore transparency entry: 1572126404
- Sigstore integration time:
-
Permalink:
timrburnham/paperjson@c917aae19f378ed0d9172d3835b0e742d2a4bbf8 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/timrburnham
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c917aae19f378ed0d9172d3835b0e742d2a4bbf8 -
Trigger Event:
release
-
Statement type: