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.6.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.6.tar.gz.
File metadata
- Download URL: json_tag-0.1.6.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 |
9bf3a9a7e69c1b7fb4a46206104b8fe7c70abc55a3a6e63653a644f6919a6b65
|
|
| MD5 |
0def8f60c48ad2f97c635fdf98447f63
|
|
| BLAKE2b-256 |
e607e9e6d7b2ea15a19d695e77b11383a4a1ab25650a1911cbc826c5e19d44b0
|
File details
Details for the file json_tag-0.1.6-py3-none-any.whl.
File metadata
- Download URL: json_tag-0.1.6-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 |
cd10343bca2d33977bd629cb407cf2ae77ae507ee494582f2238249286f2e156
|
|
| MD5 |
759deabe45aced21eb72ce303a413e09
|
|
| BLAKE2b-256 |
03c9a6419067f92cff5a5efc0474094e6578b15a6479863e0d37455f85d0c5af
|