Skip to main content

A decorator to serialize a string into a Pydantic model with error handling

Project description

Pydantic deserializer

this is a simple decorator that can be used on functions that have a return type of str, To deserialize it into a given Pydantic Model.

Usage:

For a function returning a string:

def func_returning_str() -> str:
    return '{"sample": "json"}'

Let's say there's a Pydantic model like so:

class SampleJson(BaseModel):
    sample: str

We can try and parse it with the decorator:

from pydantic_serializer import deserialize

@deserialize(return_type=SampleJson)
def func_returning_str() -> str:
    return '{"sample": "json"}'

The function will return a None and log the error using loguru if there is an error. If no error occurs, then you should get a return type of SampleJson in this case.

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_serializer-0.2.1.tar.gz (1.5 kB view details)

Uploaded Source

Built Distribution

pydantic_serializer-0.2.1-py3-none-any.whl (2.2 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_serializer-0.2.1.tar.gz.

File metadata

  • Download URL: pydantic_serializer-0.2.1.tar.gz
  • Upload date:
  • Size: 1.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for pydantic_serializer-0.2.1.tar.gz
Algorithm Hash digest
SHA256 fd076b631c3fa4df6c65bfe75bd7622fb7de88b917f7d31c119b4a7e28451706
MD5 5685f731771b6e3b5a84ffbeca66077a
BLAKE2b-256 39adbd0d004a01e9a094e63b6d24d1873aad5d54a40543db3665056897bfe56e

See more details on using hashes here.

File details

Details for the file pydantic_serializer-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_serializer-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 407b1599917d6d0ca05ae53821836a99ecf1036eca07a06be9b64804e7bc2cc3
MD5 6d4e36f608e49243a2023f4a14c6058d
BLAKE2b-256 eb7570978e0554ae3508c606399af95965387d287650158c1299e4baf59522dc

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