Skip to main content

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


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 details)

Uploaded Source

Built Distribution

pydantic_decoder-1.0.2-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_decoder-1.0.2.tar.gz.

File metadata

  • Download URL: pydantic_decoder-1.0.2.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.8

File hashes

Hashes for pydantic_decoder-1.0.2.tar.gz
Algorithm Hash digest
SHA256 146d455fc805900f4f26a0a462e6503b45840abe60128445eab58c8be5649ae3
MD5 9dba0f21f78b0fe6a8334f8843712e82
BLAKE2b-256 bfef5975aac397a9998a31fb07ff8622c57942ca61e9f4c4df8913c674ad3fe9

See more details on using hashes here.

File details

Details for the file pydantic_decoder-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pydantic_decoder-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.8

File hashes

Hashes for pydantic_decoder-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b25539b9d78e547b97008781788f29757b80f86d1e3cd055736f4e7dc4792887
MD5 ca7ae56660883537ac437aea8868f9ce
BLAKE2b-256 3e2b820955541e56f7d027da36035abef216d0ba8625252d2353f5bfc9358c2c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page