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.7.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.7.tar.gz.
File metadata
- Download URL: json_tag-0.1.7.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 |
cf2b5b708d3663b04e7da9d02f0bbce50f3d442890fae6e6af41723d7b7101c5
|
|
| MD5 |
30892913be755c3a9a0ed8f3f3f58449
|
|
| BLAKE2b-256 |
dc63fbaa26eea3c4c193588f2f754a9c50bc961159ff326d2de23757331e036d
|
File details
Details for the file json_tag-0.1.7-py3-none-any.whl.
File metadata
- Download URL: json_tag-0.1.7-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 |
848c757fdc5f92d447df4afcd31c13f9ce658161b58506f371501ed8af5c7cc7
|
|
| MD5 |
b2dc472e438dbf93a4097d2449bf58b6
|
|
| BLAKE2b-256 |
d334564463d119dcb6ce4f80fd9c2304cb5e8a8f4b92f077c89377c239daba5b
|