Serialize Pydantic models and non-JSONable types into a JSONable variable.
Project description
JsonAblr
JsonAblr is a Python library that allows you to encode variables into a format that represents a JSON object. It provides a simple and intuitive way to convert variables into a JSON-like structure, making it easier to store and transmit data.
This was heavily inspired by FastAPI's jsonable_encoder.
- JsonAblr now requires Pydantic 2.0 or above. If you are using Pydantic 1.x, please use JsonAblr 1.1.1.
Features
- Encode variables into a JSON-like format
- Supports various data types including strings, numbers, lists, dictionaries, dates, Pydantic models, dataclasses and more
- Easy to use
- Customizable for your own types
Installation
You can install Jsonablr using pip:
pip install jsonablr
Usage
encode helper function:
from jsonablr import encode
from datetime import datetime, timezone
my_date = datetime.now(tz=timezone.utc)
print(encode(my_date))
Output:
2023-06-26T12:30:00.000Z
JsonAblr class:
from typing import Set
from datetime import datetime
from jsonablr import JsonAblr
from pydantic import BaseModel
class DynamoDbItem(BaseModel):
partition_key: str
sort_key: str
games: Set[str]
when: datetime
encoder = JsonAblr(
encoders={
datetime: lambda x: x.isoformat(sep=' ', timespec='seconds')
},
preserve_set=True
)
item = DynamoDbItem(
partition_key='foo',
sort_key='bar',
games=['minecraft', 'terraria'],
when=datetime.now()
)
print(encoder.encode(item))
This example demonstrates using a Pydantic model to be used as AWS DynamoDB item and not to be used as JSON. The games attribute is returned as a set (when using the preserve_set=True option) to support the DynamoDB SET type.
Serialising a set with json.dumps will cause an error.
Output:
{
'partition_key': 'foo',
'sort_key': 'bar',
'games': {'minecraft', 'terraria'},
'when': '2023-06-26 12:30:00'
}
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
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 jsonablr-2.0.1.tar.gz.
File metadata
- Download URL: jsonablr-2.0.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/22.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96305b3a25ae0ca75b97baa17e1c81a89956d24d621f8a56f541836aea9a61db
|
|
| MD5 |
7c6b37a912197d73a6bf5f3c08f02ee4
|
|
| BLAKE2b-256 |
7c60c1f36f45631ea248680d4b99357deb29670831118f394338072a8b84d012
|
File details
Details for the file jsonablr-2.0.1-py3-none-any.whl.
File metadata
- Download URL: jsonablr-2.0.1-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/22.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4076ccecf537f87d74e2e82fa63cae12a071a2cdd0b0034ebf2f6f6ecd38c687
|
|
| MD5 |
f1cf2b685a07078926f556609366ffe1
|
|
| BLAKE2b-256 |
02b25c19d393394293665ebd307fae9a4f9f28dbe73ba6e5eb1d4537ed55ac8c
|