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.4.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.4.tar.gz.
File metadata
- Download URL: json_tag-0.1.4.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 |
3d601a96fffba3d2746608baf248739057f82a7f991248400dc5b97b9f4463b6
|
|
| MD5 |
b98de4e25223e911181e7b787458aca6
|
|
| BLAKE2b-256 |
9d33166b346db775704c71cb51435b429bd14017779e5ee40bd5d7caa618ab13
|
File details
Details for the file json_tag-0.1.4-py3-none-any.whl.
File metadata
- Download URL: json_tag-0.1.4-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 |
92bb63b8d84a735283b7d762d43ff691692c1a4e4aa0a9bebb11052111444c5a
|
|
| MD5 |
a13b11d43e5c625674ab866f3534edf6
|
|
| BLAKE2b-256 |
0b9abd4a1e385eb8324b60d463a2b5a3b03840c30e2e1d673b1184d87d2b7c74
|