Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more object types in BetterJsonEncoder #62

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/fastapi_redis_cache/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import json
from datetime import date, datetime
from decimal import Decimal
from uuid import UUID
from enum import Enum
from pydantic import BaseModel

from dateutil import parser

Expand Down Expand Up @@ -28,6 +31,12 @@ def default(self, obj):
return {"val": obj.strftime(DATE_ONLY), "_spec_type": str(date)}
elif isinstance(obj, Decimal):
return {"val": str(obj), "_spec_type": str(Decimal)}
elif isinstance(obj, BaseModel):
return obj.dict()
elif isinstance(obj, UUID):
return str(obj)
elif isinstance(obj, Enum):
return str(obj.value)
else: # pragma: no cover
return super().default(obj)

Expand Down