Skip to main content

Powerful universal JSON encoder/decoder for Python objects with support for pydantic v2.

Project description

🚀 Kajson - Universal JSON Encoder/Decoder for Python

PyPI version Python versions License Discord Website

Kajson is a powerful drop-in replacement for Python's standard json module that automatically handles complex object serialization, including Pydantic v2 models, datetime objects, and custom types.

This library is used by Pipelex, the open-source language for repeatable AI workflows, check it out.

✨ Why Kajson?

Say goodbye to type X is not JSON serializable!

The Problem with Standard JSON

import json
from datetime import datetime
from pydantic import BaseModel

class User(BaseModel):
    name: str
    created_at: datetime

user = User(name="Alice", created_at=datetime.now())

# ❌ Standard json fails
json.dumps(user)  # TypeError: Object of type User is not JSON serializable

The Kajson Solution

Full example: ex_08_readme_basic_usage.py

import kajson

# ✅ Just works!
json_str = kajson.dumps(user)
restored_user = kajson.loads(json_str)
assert user == restored_user  # Perfect reconstruction!

🎯 Key Features

  • 🔄 Drop-in replacement - Same API as standard json module
  • 🐍 Pydantic v2 support - Seamless serialization of Pydantic models
  • 📅 DateTime handling - Built-in support for date, time, datetime, timedelta
  • 🏗️ Type preservation - Automatically preserves and reconstructs original types
  • 🔌 Extensible - Easy registration of custom encoders/decoders
  • 🎁 Batteries included - Common types work out of the box

📦 Installation

# Using pip
pip install kajson

# Using poetry
poetry add kajson

# Using uv (recommended)
uv pip install kajson

🚀 Quick Start

Basic Usage

Full example: ex_01_basic_pydantic_serialization.py

from datetime import datetime
from pydantic import BaseModel
from kajson import kajson, kajson_manager

class User(BaseModel):
    name: str
    email: str
    created_at: datetime

def main():
    # Create and serialize
    user = User(
        name="Alice", 
        email="alice@example.com", 
        created_at=datetime.now()
    )

    # Serialize to JSON
    json_str = kajson.dumps(user, indent=2)

    # Deserialize back
    restored_user = kajson.loads(json_str)
    assert user == restored_user  # ✅ Perfect reconstruction!

if __name__ == "__main__":
    kajson_manager.KajsonManager()
    main()

Working with Complex Nested Models

Full example: ex_09_readme_complex_nested.py

from datetime import datetime
from typing import Any, Dict, List
from pydantic import BaseModel

class Comment(BaseModel):
    author: str
    content: str
    created_at: datetime

class BlogPost(BaseModel):
    title: str
    content: str
    published_at: datetime
    comments: List[Comment]
    metadata: Dict[str, Any]

# Create complex nested structure
post = BlogPost(
    title="Introducing Kajson",
    content="A powerful JSON library...",
    published_at=datetime.now(),
    comments=[
        Comment(author="Alice", content="Great post!", created_at=datetime.now()),
        Comment(author="Bob", content="Very helpful", created_at=datetime.now())
    ],
    metadata={"views": 1000, "likes": 50}
)

# Serialize and deserialize - it just works!
json_str = kajson.dumps(post)
restored_post = kajson.loads(json_str)

# All nested objects are perfectly preserved
assert isinstance(restored_post.comments[0], Comment)
assert restored_post.comments[0].created_at.year == datetime.now().year

🔧 Advanced Features

Custom Type Registration

Full example: ex_10_readme_custom_registration.py

Register encoders/decoders for any type:

from decimal import Decimal
from pathlib import Path
from typing import Any, Dict
import kajson

# Register Decimal support
def encode_decimal(value: Decimal) -> Dict[str, str]:
    return {"decimal": str(value)}

def decode_decimal(data: Dict[str, str]) -> Decimal:
    return Decimal(data["decimal"])

kajson.UniversalJSONEncoder.register(Decimal, encode_decimal)
kajson.UniversalJSONDecoder.register(Decimal, decode_decimal)

# Now Decimal works seamlessly
data = {"price": Decimal("19.99"), "tax": Decimal("1.50")}
json_str = kajson.dumps(data)
restored = kajson.loads(json_str)
assert restored["price"] == Decimal("19.99")  # ✅

# Register Path support
kajson.UniversalJSONEncoder.register(
    Path, 
    lambda p: {"path": str(p)}
)
kajson.UniversalJSONDecoder.register(
    Path, 
    lambda d: Path(d["path"])
)

# Path objects now work too!
config = {"home": Path.home(), "config": Path("/etc/myapp/config.json")}
restored_config = kajson.loads(kajson.dumps(config))

Custom Classes with Hooks

Full example: ex_11_readme_custom_hooks.py

Add JSON support to your own classes:

from typing import Any, Dict
from typing_extensions import override

class Vector:
    def __init__(self, x: float, y: float):
        self.x = x
        self.y = y
    
    def __json_encode__(self):
        """Called by Kajson during serialization"""
        return {"x": self.x, "y": self.y}
    
    @classmethod
    def __json_decode__(cls, data: Dict[str, Any]):
        """Called by Kajson during deserialization"""
        return cls(data["x"], data["y"])
    
    @override
    def __eq__(self, other: object) -> bool:
        if not isinstance(other, Vector):
            return False
        return self.x == other.x and self.y == other.y

# Works automatically!
vector = Vector(3.14, 2.71)
restored = kajson.loads(kajson.dumps(vector))
assert vector == restored

Working with Mixed Types

Full example: ex_12_readme_mixed_types.py

from datetime import datetime, timedelta
from typing import Any, Dict
from pydantic import BaseModel

class Task(BaseModel):
    name: str
    created_at: datetime
    duration: timedelta
    metadata: Dict[str, Any]

# Create mixed-type list
tasks = [
    Task(
        name="Data processing",
        created_at=datetime.now(),
        duration=timedelta(hours=2, minutes=30),
        metadata={"priority": "high", "cpu_cores": 8}
    ),
    {"raw_data": "Some plain dict"},
    datetime.now(),
    ["plain", "list", "items"],
]

# Kajson handles everything!
json_str = kajson.dumps(tasks)
restored_tasks = kajson.loads(json_str)

# Type checking shows proper reconstruction
assert isinstance(restored_tasks[0], Task)
assert isinstance(restored_tasks[0].duration, timedelta)
assert isinstance(restored_tasks[2], datetime)

🛡️ Error Handling

Full example: ex_13_readme_error_handling.py

Kajson provides clear error messages for validation issues:

from pydantic import BaseModel, Field

class Product(BaseModel):
    name: str
    price: float = Field(gt=0)  # Price must be positive

# Invalid data
json_str = '{"name": "Widget", "price": -10, "__class__": "Product", "__module__": "__main__"}'

try:
    product = kajson.loads(json_str)
except kajson.KajsonDecoderError as e:
    print(f"Validation failed: {e}")
    # Output: Validation failed: Could not instantiate pydantic BaseModel...

🤝 Compatibility

  • Python: 3.9+
  • Pydantic: v2.x
  • Dependencies: Minimal, only standard library + pydantic

🔄 Migration from Standard JSON

Migrating is as simple as changing your import:

# Before
import json
data = json.dumps(my_object)  # Often fails with complex objects

# After  
import kajson as json  # Drop-in replacement!
data = json.dumps(my_object)  # Works with complex objects

Or use Kajson's convenience functions directly:

import kajson
data = kajson.dumps(my_object)

🏗️ How It Works

Kajson extends the standard JSON encoder/decoder by:

  1. Type Preservation: Adds __class__ and __module__ metadata to JSON objects
  2. Smart Decoding: Automatically reconstructs original Python objects
  3. Registry System: Allows registration of custom encoders/decoders
  4. Pydantic Integration: Special handling for Pydantic models and validation

📚 Use Cases

  • REST APIs: Serialize Pydantic models for API responses
  • Data Persistence: Save complex objects to JSON files
  • Message Queues: Send rich objects through Redis/RabbitMQ
  • Configuration: Store config with proper types
  • Data Science: Serialize numpy arrays, pandas DataFrames (with custom encoders)

🔗 Examples

All code examples from this README are available as executable files in the examples/ directory:

Run any example with:

cd examples
python ex_01_basic_pydantic_serialization.py

Credits

This project is heavily based on the excellent work from unijson by Bastien Pietropaoli and distributed under the same license, Apache 2.0.

License

© 2025 Evotis S.A.S. - Licensed under Apache 2.0

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

kajson-0.2.0.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

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

kajson-0.2.0-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file kajson-0.2.0.tar.gz.

File metadata

  • Download URL: kajson-0.2.0.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for kajson-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1dbe9595ef70a6cb1e3fdfc28fa6f1f3e361bbdfb03b0a4ca525f3f6f6100b92
MD5 617bc50575ac82431411ddf11fbc0adb
BLAKE2b-256 8ab5404dda6ea283cba04e408d4447466a14863586f6872f87d70883e32ac48a

See more details on using hashes here.

Provenance

The following attestation bundles were made for kajson-0.2.0.tar.gz:

Publisher: publish-pypi.yml on Pipelex/kajson

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kajson-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: kajson-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 24.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for kajson-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1221d4d3be54033f97642c4df9a7144d086a49d95f1f2adeb975308e671c5ac9
MD5 9de33f8acd3cbb4a9742234de1cc7f6b
BLAKE2b-256 1eda8942c19fb6806d29d8769059a2f5f0a19bd9cbaadaaad725dd1aae8efa78

See more details on using hashes here.

Provenance

The following attestation bundles were made for kajson-0.2.0-py3-none-any.whl:

Publisher: publish-pypi.yml on Pipelex/kajson

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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