Skip to main content

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'
}

Metadata

Release files for jsonablr 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsonablr 2.0.1
File Size Uploaded Provenance Actions
jsonablr-2.0.1.tar.gz 4.2 kB Without provenance Details for jsonablr-2.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for jsonablr 2.0.1
File Interpreter ABI Platform Actions
jsonablr-2.0.1-py3-none-any.whl Python 3 none any Details for jsonablr-2.0.1-py3-none-any.whl

Total release size: 8.9 kB

Release files / jsonablr-2.0.1.tar.gz

Download URL jsonablr-2.0.1.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
96305b3a25ae0ca75b97baa17e1c81a89956d24d621f8a56f541836aea9a61db
BLAKE2b-256 checksum
How to use checksums
7c60c1f36f45631ea248680d4b99357deb29670831118f394338072a8b84d012
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.4 Darwin/22.5.0

Release files / jsonablr-2.0.1-py3-none-any.whl

Download URL jsonablr-2.0.1-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4076ccecf537f87d74e2e82fa63cae12a071a2cdd0b0034ebf2f6f6ecd38c687
BLAKE2b-256 checksum
How to use checksums
02b25c19d393394293665ebd307fae9a4f9f28dbe73ba6e5eb1d4537ed55ac8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.4 Darwin/22.5.0

Security for jsonablr 2.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page