Skip to main content

JSON Decoder / Encoder for type hints

Simple, 2 file python module that parses JSON into dataclasses / enums and vice versa. Ignores extra fields, and works with default values for missing fields.

Requires Python 3.8.0 or higher

For security sake, you have to know the type you're expecting to deserialize into.

Unions will be resolved in order (i.e. Union[int, MyType, None] will first try parsing as int, then as MyType, then as None), so if a value is MULTIPLE valid types, it might not pick the right one.

Installation

pip install typehintjson

Usage

import json
import typing as T

from typehintjson import parse_as_type, dataclass_to_dictionary
from dataclasses import dataclass

@dataclass
class Child:
    x: T.Union[int, str]
    y: int = 4

@dataclass
class Parent:
    a: int
    c: Child


obj = Parent(a=2, c=Child(x="meow"))
print(repr(parse_as_type(json.loads(json.dumps(dataclass_to_dictionary(obj))), Parent)))

# Parent(a=2, c=Child(x='meow', y=4))

Metadata

Release files for typehintjson 1.0.4

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

Source distribution (sdist)

Source distribution for typehintjson 1.0.4
File Size Uploaded
typehintjson-1.0.4.tar.gz 3.1 kB Details

Built distribution (wheel)

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

Total release size: 7.0 kB

Release files / typehintjson-1.0.4.tar.gz

Download URL typehintjson-1.0.4.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f77ad97c2bbccbcd002ce7a51555f76528bc542d9292e702662a3feb64a6b7c0
BLAKE2b-256 checksum
How to use checksums
84a02c3dd196bfa19f4b2c62d08634adb5ffe2aada71dff3914a4c7ee41748ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.4

Release files / typehintjson-1.0.4-py3-none-any.whl

Download URL typehintjson-1.0.4-py3-none-any.whl
Size 3.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8599b9c96aa00dcdfe554cc37c24c63e5bddca87388466c86fc28da073e1909a
BLAKE2b-256 checksum
How to use checksums
59c1ebd3acd286c0cc335d22e92bc16f11a63851b4309356022eb8869732407d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.4

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

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