justapi — /docs

The Problem

When you need to convert Pydantic models to JSON-compatible dictionaries (for example, before passing to a database driver), you can't just call model_dump() — it doesn't handle datetime, UUID, or other non-JSON types.

JustAPI's Encoder

from justapi import JustAPIApp
from pydantic import BaseModel
from datetime import datetime

class Event(BaseModel):
    name: str
    starts: datetime

app = JustAPIApp()

@app.post("/events")
def create_event(event: Event):
    # Convert to JSON-compatible dict
    data = event.model_dump()
    # starts is a datetime object — need to serialize it
    return {"event_name": data["name"], "starts": data["starts"].isoformat()}

Model_dump with mode

Pydantic v2 provides model_dump() with serialization modes:

@app.post("/events")
def create_event(event: Event):
    data = event.model_dump(mode="json")
    # data["starts"] is now an ISO string
    return data

See Also

JustAPI v2.0.9 — Open Source MIT License · Built with WebTUI · GitHub

NORMAL master justapi/docs
utf-8 Top 1:1