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 = model.json()
m = model.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.2.tar.gz
(5.1 kB
view hashes)
Built Distribution
Close
Hashes for pydantic_decoder-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b25539b9d78e547b97008781788f29757b80f86d1e3cd055736f4e7dc4792887 |
|
MD5 | ca7ae56660883537ac437aea8868f9ce |
|
BLAKE2b-256 | 3e2b820955541e56f7d027da36035abef216d0ba8625252d2353f5bfc9358c2c |