Convert json text back into proper Pydantic models.
Project description
Convert json text back into proper Pydantic models.
Simple Example
from pydantic_decoder import DecoderModel, encode, decode
class MyModel(DecoderModel):
first_name: str
last_name: str
model = MyModel(first_name='John', last_name='Doe')
msg = encode(model)
m = decode(msg)
assert m == model
assert isinstance(m, MyModel)
Registration Examples
Use your current models by only changing the inheritance from pydantic.BaseModel to pydantic_decoder.DecoderModel.
from pydantic_decoder import DecoderModel, register_model, encode, decode
class MyModel(DecoderModel):
first_name: str
last_name: str
class MyModel2(DecoderModel):
first_name: str
last_name: str
# Subclasses of the DecoderModel are registered automatically
# register_model(MyModel)
# register_model('Model2', MyModel2)
model = MyModel(first_name='John', last_name='Doe')
msg = encode(model)
m = decode(msg)
assert m == model
assert isinstance(m, MyModel)
model2 = MyModel2(first_name='John', last_name='Doe')
msg = encode(model2)
m2 = decode(msg)
assert m2 == model2
assert isinstance(m2, MyModel2)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pydantic_decoder-1.0.1.tar.gz
(6.3 kB
view hashes)
Built Distribution
Close
Hashes for pydantic_decoder-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08dc8c1eec2779ce2f1005e82601a922ba1438ebe2bc47ea0e7952e1d9d98951 |
|
MD5 | 424b364582d75ab240807136fd279cea |
|
BLAKE2b-256 | c223c8fbeabf363be69dfeecb687d3d9bdbd14940204c195c1d40d764eb167f5 |