fastcheck/fastcheck_api/app/utils/mongo.py
2026-04-16 12:05:29 -04:00

24 lines
628 B
Python

from __future__ import annotations
from collections.abc import Mapping, Sequence
from datetime import datetime
from typing import Any
from bson import ObjectId
def to_object_id(value: str) -> ObjectId:
return ObjectId(value)
def jsonable(value: Any) -> Any:
if isinstance(value, ObjectId):
return str(value)
if isinstance(value, datetime):
return value
if isinstance(value, Mapping):
return {k: jsonable(v) for k, v in value.items()}
if isinstance(value, Sequence) and not isinstance(value, (str, bytes, bytearray)):
return [jsonable(v) for v in value]
return value