Skip to main content

JSON encoder that handles datetime, Decimal, UUID, dataclasses, and sets without crashing

Project description

philiprehberger-safe-json

Tests PyPI version License

JSON encoder that handles datetime, Decimal, UUID, dataclasses, and sets without crashing.

Install

pip install philiprehberger-safe-json

Usage

from philiprehberger_safe_json import dumps, loads

data = {
    "created": datetime(2026, 3, 13, 12, 0, 0),
    "price": Decimal("19.99"),
    "id": UUID("12345678-1234-5678-1234-567812345678"),
    "tags": {"beta", "release"},
}

json_string = dumps(data)
parsed = loads(json_string)

Datetime and Date

from datetime import datetime, date
from philiprehberger_safe_json import dumps

dumps({"timestamp": datetime(2026, 1, 15, 9, 30, 0)})
# '{"timestamp": "2026-01-15T09:30:00"}'

dumps({"day": date(2026, 1, 15)})
# '{"day": "2026-01-15"}'

Decimal

from decimal import Decimal
from philiprehberger_safe_json import dumps

dumps({"price": Decimal("9.99")})
# '{"price": 9.99}'

dumps({"price": Decimal("9.99")}, decimal_as_string=True)
# '{"price": "9.99"}'

UUID

from uuid import UUID
from philiprehberger_safe_json import dumps

dumps({"id": UUID("abcdef01-2345-6789-abcd-ef0123456789")})
# '{"id": "abcdef01-2345-6789-abcd-ef0123456789"}'

Dataclasses

from dataclasses import dataclass
from philiprehberger_safe_json import dumps

@dataclass
class User:
    name: str
    age: int

dumps({"user": User(name="Alice", age=30)})
# '{"user": {"name": "Alice", "age": 30}}'

Sets and Frozensets

from philiprehberger_safe_json import dumps

dumps({"tags": {"c", "a", "b"}})
# '{"tags": ["a", "b", "c"]}'

Bytes

from philiprehberger_safe_json import dumps

dumps({"data": b"hello"})
# '{"data": "aGVsbG8="}'

Enums

from enum import Enum
from philiprehberger_safe_json import dumps

class Color(Enum):
    RED = "red"
    BLUE = "blue"

dumps({"color": Color.RED})
# '{"color": "red"}'

Path

from pathlib import Path
from philiprehberger_safe_json import dumps

dumps({"file": Path("/home/user/data.txt")})
# '{"file": "/home/user/data.txt"}'

Using SafeJsonEncoder Directly

import json
from philiprehberger_safe_json import SafeJsonEncoder

json.dumps({"key": some_value}, cls=SafeJsonEncoder)

API

Name Description
SafeJsonEncoder json.JSONEncoder subclass that handles datetime, date, Decimal, UUID, dataclass, set, frozenset, bytes, Enum, and Path
SafeJsonEncoder.decimal_as_string Class attribute; when True, Decimal values serialize as strings instead of floats (default: False)
dumps(obj, *, decimal_as_string=False, **kwargs) Serialize to JSON string using SafeJsonEncoder. Accepts all json.dumps keyword arguments
loads(s, **kwargs) Deserialize a JSON string. Pass-through to json.loads for API symmetry

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

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

philiprehberger_safe_json-0.1.3.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_safe_json-0.1.3-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_safe_json-0.1.3.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_safe_json-0.1.3.tar.gz
Algorithm Hash digest
SHA256 255f6a01eb83146b7a48c5e504f158f1303b6136cdad4a7cea1a76aa6eebe80b
MD5 e251fd99199fd397526fd65f042b2112
BLAKE2b-256 2c4ff09b835565ed8579f01ca768c4ad9913d0f61b3f549a859736f9f37b8cb4

See more details on using hashes here.

File details

Details for the file philiprehberger_safe_json-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_safe_json-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ae1f88b5b6c2c452a0699ad1a887cb1962ebbe5125bd50b6d49676f0771f3b02
MD5 d434c173c2ae17c5d4bc3cef83b08848
BLAKE2b-256 a8ee6eb16c91bb5c5e66f462a2496ade0e0af4ff16ad94000a8b2732f7a58252

See more details on using hashes here.

Supported by

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