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

Uploaded Python 3

File details

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

File metadata

  • Download URL: json_tag-0.1.5.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.5.tar.gz
Algorithm Hash digest
SHA256 cdf186f32d0753f6d1c9d98ca182058c998e9cafdba4a8cf72c8c96f35c32649
MD5 fb3dc5b9675251dba7a02019772b3557
BLAKE2b-256 5f242fe59879a7eedf229c3da79ccaabd58052fb45b3485cc671ac0d7fc79a03

See more details on using hashes here.

File details

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

File metadata

  • Download URL: json_tag-0.1.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a26633e8ff8f36ce184955d16e0d208d31a6bc71dcd8e805b51723bc918fb66f
MD5 af51fdb057a7395329f29fc8d5aae5ad
BLAKE2b-256 5a5640d8401c56de2b1358ea5d0a9e8c9548b2c15713065566675229399fcdd0

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