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.2.tar.gz
(3.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json_tag-0.1.2.tar.gz.
File metadata
- Download URL: json_tag-0.1.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7acdb4fb0ac07a3ff0e1958f68fef3ab281d3cdc776c46a581907e2dc4d4fd80
|
|
| MD5 |
a995d5f8e1a1a2fbfcc272dbdc9feecb
|
|
| BLAKE2b-256 |
7fff2c2babeb0d780fb744859c748024d30e2bf476e2a6cce98edb8bd3ebfeaf
|
File details
Details for the file json_tag-0.1.2-py3-none-any.whl.
File metadata
- Download URL: json_tag-0.1.2-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b1123d80099bfa87ffb1f5e735aa53caa579854f1108701d681874faa7921bb
|
|
| MD5 |
63db2bdf5b319b0b34c34f70ea07534c
|
|
| BLAKE2b-256 |
ea7f2ee0fcfd1afe7eb6f123d9b20d635f3a0c4022560322a7edaf1886b4888f
|