Kajson: Drop-in JSON replacement with Pydantic v2, polymorphism and type preservation

What My Project Does

Ever spent hours debugging « Object of type X is not JSON serializable »? Yeah, me too. Kajson fixes that nonsense: just swap import json with import kajson as json and watch your Pydantic models, datetime objects, enums, and entire class hierarchies serialize like magic.

Polymorphism that just works: Got a Pet with an Animal field? Kajson remembers if it’s a Dog or Cat when you deserialize. No discriminators, no unions, no BS. Your existing code stays untouched: Same dumps() and loads() you know and love Built for real systems: Full Pydantic v2 validation on the way back in – because production data is messy

Target Audience

This is for builders shipping real stuff: FastAPI teams, microservice architects, anyone who’s tired of writing yet another custom encoder.

AI/LLM developers doing structured generation: When your LLM spits out JSON conforming to dynamically created Pydantic schemas, Kajson handles the serialization/deserialization dance across your distributed workers. No more manually reconstructing BaseModels from tool calls.

Already battle-tested: We built this at Pipelex because our AI workflow engine needed to serialize complex model hierarchies across distributed workers. If it can handle our chaos, it can handle yours.

Comparison

stdlib json: Forces you to write custom encoders for every non-primitive type

→ Kajson handles datetime, Pydantic models, and registered types automatically

Pydantic’s .model_dump(): Stops at the first non-model object and loses subclass information

→ Kajson preserves exact subclasses through polymorphic fields – no discriminators needed

Speed-focused libs (orjson, msgspec): Optimize for raw performance but leave type reconstruction to you

→ Kajson trades a bit of speed for correctness and developer experience with automatic type preservation

Schema-first frameworks (Marshmallow, cattrs): Require explicit schema definitions upfront

→ Kajson works immediately with your existing Pydantic models – zero configuration needed

Each tool has its sweet spot. Kajson fills the gap when you need type fidelity without the boilerplate.

Source Code Link

https://github.com/Pipelex/kajson

Getting Started

pip install kajson

Simple example with some tricks mixed in:

from datetime import datetime from enum import Enum from pydantic import BaseModel import kajson as json # 👈 only change needed # Define an enum class Personality(Enum): PLAYFUL = « playful » GRUMPY = « grumpy » CUDDLY = « cuddly » # Define a hierarchy with polymorphism class Animal(BaseModel): name: str class Dog(Animal): breed: str class Cat(Animal): indoor: bool personality: Personality class Pet(BaseModel): acquired: datetime animal: Animal # ⚠️ Base class type! # Create instances with different subclasses fido = Pet(acquired=datetime.now(), animal=Dog(name= »Fido », breed= »Corgi »)) whiskers = Pet(acquired=datetime.now(), animal=Cat(name= »Whiskers », indoor=True, personality=Personality.GRUMPY)) # Serialize and deserialize – subclasses and enums preserved automatically! whiskers_json = json.dumps(whiskers) whiskers_restored = json.loads(whiskers_json) assert isinstance(whiskers_restored.animal, Cat) # ✅ Still a Cat, not just Animal assert whiskers_restored.animal.personality == Personality.GRUMPY ✅ ✓ Enum preserved assert whiskers_restored.animal.indoor is True # ✅ All attributes intact

Credits

Built on top of the excellent unijson by Bastien Pietropaoli. Standing on the shoulders of giants here.

Call for Feedback

What’s your serialization horror story?

If you give Kajson a spin, I’d love to hear how it goes! Does it actually solve a problem you’re facing? How does it stack up against whatever serialization approach you’re using now? Always cool to hear how other devs are tackling these issues, might learn something new myself. Thanks!

submitted by /u/lchoquel to r/Python
[link] [comments]


Commentaires

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *