Skip to main content

Python pydantic wrapper for JSON# — the #type convention for cross-language type tagging in JSON.

Project description

json-tag

Python support for JSON#.

Configures pydantic to use the JSON# convention.

Install

pip install json-tag

Usage

Define your types:

class Circle(JsonTagModel):
    radius: float

class Rectangle(JsonTagModel):
    width: float
    height: float

Shape = tagged_union(Circle, Rectangle)

Serialize:

circle = Circle(radius=4.0)
circle.to_json()
# {"#type":"Circle","radius":4}

Deserialize:

from pydantic import TypeAdapter

shape = TypeAdapter(Shape).validate_json('{"#type":"Circle","radius":4}')
# Circle(radius=4.0)

Custom tag names via __json_tag_name__ class variable:

class PersonalVehicle(JsonTagModel):
    __json_tag_name__ = "Car"
    make: str

API

Export Description
JsonTagModel Base model — subclasses get #type automatically
tagged_union(*types) Create a discriminated union with #type
TYPE_FIELD The "#type" constant

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json_tag-1.0.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_tag-1.0.0-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file json_tag-1.0.0.tar.gz.

File metadata

  • Download URL: json_tag-1.0.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for json_tag-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b6cfde2eee13ea0b58c7f4eed6cc59f1f68e603e117797fc3404b584eb745127
MD5 33a354ca2b315c412b1de47ca61881cc
BLAKE2b-256 8e9454676b693d620a0b4244cdcdcc82012f3c1ebe0ec77a82b4f697745c90a5

See more details on using hashes here.

File details

Details for the file json_tag-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: json_tag-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for json_tag-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f33c3536bb9a9b8daf646aec53738af9a8d8f459e5ba1d3486c0e19666b9745c
MD5 cfd11ac69ef3c3b320809c3d3525477a
BLAKE2b-256 c5151df4d59c16c34c73b5d730638a32e953666a4d98edb61c5f32e4eff8ee0a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page