Skip to main content

Fast and secure JSON serializer for Redis caching with Pydantic and dataclass support

Project description

redis-json-serializer

Fast and secure JSON serializer for Redis caching with Pydantic and dataclass support.

Features

  • 🚀 Fast: Built on top of orjson for maximum performance
  • 🔒 Secure: Whitelist-based model registration prevents arbitrary code execution
  • 📦 Type-safe: Full support for Pydantic models and dataclasses
  • 🔧 Extensible: Plugin-based architecture for custom types
  • 🎯 Simple: Clean API with minimal boilerplate

Installation

From PyPI (when published)

pip install redis-json-serializer

From source (development)

# Clone repository
git clone git@github.com:seligoroff/redis-json-serializer.git
cd redis-json-serializer

# Install in development mode
pip install -e ".[dev,all]"

# Or using requirements files
pip install -r requirements-dev.txt

With optional dependencies

# With Pydantic support
pip install redis-json-serializer[pydantic]

# With MongoDB ObjectId support
pip install redis-json-serializer[mongodb]

# With aiocache integration
pip install redis-json-serializer[aiocache]

# All optional dependencies
pip install redis-json-serializer[all]

Quick Start

Basic Usage

from redis_json_serializer import JsonSerializer

serializer = JsonSerializer()

# Serialize
data = {"name": "John", "age": 30, "tags": {"python", "redis"}}
packed = serializer.pack(data)
json_bytes = serializer.dumps(data)

# Deserialize
unpacked = serializer.unpack(serializer.loads(json_bytes))

With Pydantic Models

from redis_json_serializer import JsonSerializer, register_model
from pydantic import BaseModel

@register_model("user.v1")  # Stable alias for versioning
class User(BaseModel):
    id: str
    name: str
    email: str

serializer = JsonSerializer()

user = User(id="123", name="Alice", email="alice@example.com")
packed = serializer.pack(user)
unpacked = serializer.unpack(packed)  # Returns User instance

With aiocache

from aiocache import cached
from aiocache.serializers import JsonSerializer
from redis_json_serializer import JsonSerializer as RedisJsonSerializer

# Configure aiocache to use our serializer
from aiocache import caches
caches.set_config({
    "default": {
        "cache": "aiocache.RedisCache",
        "serializer": {
            "class": "redis_json_serializer.aiocache.AiocacheJsonSerializer",
        },
    }
})

@cached(ttl=3600)
async def get_user(user_id: str) -> User:
    # Your logic here
    return User(id=user_id, name="Alice", email="alice@example.com")

Architecture

The library uses a dispatch-table approach for O(1) type lookup instead of chain-of-responsibility pattern, providing better performance and simpler code.

Supported Types

  • Native JSON: str, int, float, bool, None
  • Dates: datetime.datetime, datetime.date
  • Collections: set, list, tuple, dict
  • Pydantic models: With registration via @register_model()
  • Dataclasses: With registration via @register_model()
  • Custom types: Decimal, ObjectId (MongoDB)

Versioning

The library supports format versioning through namespaces:

serializer = JsonSerializer(namespace="cache:v2:")

This allows safe migration between format versions without clearing the entire Redis cache.

License

MIT

Contributing

Contributions are welcome! Please read our contributing guidelines first.

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

redis_json_serializer-0.1.0.tar.gz (74.2 kB view details)

Uploaded Source

Built Distribution

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

redis_json_serializer-0.1.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file redis_json_serializer-0.1.0.tar.gz.

File metadata

  • Download URL: redis_json_serializer-0.1.0.tar.gz
  • Upload date:
  • Size: 74.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for redis_json_serializer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 142d941c516cb11cf67c04a9673b36f4f6e60256f0b8d356584970fa51513c3d
MD5 6fff802b949061f01055ef5951296db1
BLAKE2b-256 403f244ea3d0fadb1bd744d57d8dc570f183ca292d5eb82b2e9f56b79dee4ccf

See more details on using hashes here.

File details

Details for the file redis_json_serializer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for redis_json_serializer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f142ec791d0444717012196da179429b5555550e1d83ebfbbce34f19204953c0
MD5 55ac1445e256042f81e84c09f378be7a
BLAKE2b-256 47f83c6fedc25d4847d1053d4496b6c857d6cfc3a361f1d0eeb75c6c7a16a8b4

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