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-0.1.1.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-0.1.1-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: json_tag-0.1.1.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-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b1409ca91cbd8c9d54b42f7bee26b5520f4493bfd904809ac9938d5e9d919e63
MD5 c7f8776c3aff946a134025993031faf4
BLAKE2b-256 b5f4c5f5477f9b695829b36728136205ef9b2e39fa659db9d6dde60667492146

See more details on using hashes here.

File details

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

File metadata

  • Download URL: json_tag-0.1.1-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-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fa975d7cc3f00f90db75c820d4f401fabb3ce7c6e7b0a5627f641458f4ffa0bc
MD5 bb94b5b75763e1d0ea590c5f165668fe
BLAKE2b-256 fecfbcfe3ca62504c03d8fc0172645f140b2ace34bd7b566a18e091d880751fd

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