De-/serialize Python dataclasses to or from JSON payloads. Compatible with Python 3.7 and newer.
Project description
databind.json
The databind.json
package implements the de-/serialization to or from JSON payloads using
the databind.core
framework.
Quickstart
import databind.json
import dataclasses
@dataclasses.dataclass
class ServerConfig:
host: str
port: int = 8080
@dataclasses.dataclass
class MainConfig:
server: ServerConfig
payload = { 'server': { 'host': '127.0.0.1' } }
config = databind.json.load(payload, MainConfig)
assert config == MainConfig(ServerConfig('127.0.0.1'))
Examples for common use cases
Unions with literal matches
import dataclasses
import databind.json
from typing import Literal
@dataclasses.dataclass
class AwsMachine:
region: str
name: str
instance_id: str
provider: Literal["aws"] = "aws"
@dataclasses.dataclass
class AzureMachine:
resource_group: str
name: str
provider: Literal["azure"] = "azure"
Machine = AwsMachine | AzureMachine
payload = {"provider": "azure", "resource_group": "foo", "name": "bar"}
assert databind.json.load(payload) == AzureMachine("foo", "bar")
Copyright © 2020 – Niklas Rosenstein
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
databind.json-2.0.9.tar.gz
(11.5 kB
view hashes)
Built Distribution
Close
Hashes for databind.json-2.0.9-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2b2e0cab8207224f78cee8460ecf590bd64244f75eb14806d5c9285a7e192b0 |
|
MD5 | 39397cf877fc6b56e3036c24e0919e33 |
|
BLAKE2b-256 | d97ff43461df9cec4963d3e0b495495b12b10f22f380dc433e073842a63a9692 |