Skip to main content

JSON Dataclasses

GitHub PyPI GitHub Workflow Status GitHub Workflow Status

Typed JSON dataclasses for Python 3.9+

Check out the docs

Installation

pip install jsondataclasses

Usage

from datetime import date
from typing import Literal, Optional

from jsondataclasses import jsondataclass, jsonfield


def parse_date(date_string: str) -> date:
    return date.fromisoformat(date_string.replace(".", "-"))


@jsondataclass
class Car:
    make: Literal["Ford", "Renault", "Volkswagen"] = jsonfield("carMake")
    model: str = jsonfield("model")
    manufactured_at: date = jsonfield("dateOfManufacture", parse_date)
    num_of_wheels: Optional[int] = jsonfield("numberOfWheels", default_value=4)


car = Car({
    "carMake": "Ford",
    "model": "Focus",
    "dateOfManufacture": "2018.03.14"
})
print(car)  # Car(make='Ford', model='Focus', manufactured_at=datetime.date(2018, 3, 14), num_of_wheels=4)

Class field types can be any primitive type (eg. str, int, datetime), a variadic generic (eg. list[str], Optional[int], Literal["hello", "world"]), or even another jsondataclass. The default_value argument of jsonfield will be used if the specified key is not found in the dictionary and the type of the class field is not Optional[...]. In this case, the value of default_value will be passed to the supplied parser function.

Metadata

Release files for jsondataclasses 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsondataclasses 0.0.6
File Size Uploaded
jsondataclasses-0.0.6.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsondataclasses 0.0.6
File Interpreter ABI Platform
jsondataclasses-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 8.0 kB

Release files / jsondataclasses-0.0.6.tar.gz

Download URL jsondataclasses-0.0.6.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b7a2c38aa02fb9a44d248c281e9d0db2d9589c46584239f9699dcc5f542c0b73
BLAKE2b-256 checksum
How to use checksums
6609aff029363fd03f66e3efa1778f64caf0459c371d907f08633a5d84a54153
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.1

Release files / jsondataclasses-0.0.6-py3-none-any.whl

Download URL jsondataclasses-0.0.6-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fe13fc53fc50997f1cd599bc2466f46c8fd433b26e4fec08f85e7f6a958d718c
BLAKE2b-256 checksum
How to use checksums
ea761d52efd934c2b2f499fad2168264df79ac31ca13d890ca44f8b8380b46a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.1

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page