Skip to content

Commit

Permalink
Use pydantic's JSON encoder for everything (#9)
Browse files Browse the repository at this point in the history
This allows us to put pydantic models inside e.g. a TypedDict and have
it serialize to JSON just fine. We could probably simplify the logic a
lot by just using this on everything, but I think I'll leave major
changes for the Pydantic v2 update.
  • Loading branch information
ljodal authored Jul 14, 2023
1 parent 977e738 commit cf89ef5
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion django_api_decorator/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,12 @@ def _get_response_encoder(*, type_annotation: Annotation) -> ResponseEncoder:


def _json_encoder(*, payload: Any, status: int) -> HttpResponse:
return JsonResponse(payload, status=status, safe=False)
return JsonResponse(
payload,
status=status,
json_dumps_params={"default": pydantic_encoder},
safe=False,
)


def _pydantic_encoder(payload: Any, status: int) -> HttpResponse:
Expand Down

0 comments on commit cf89ef5

Please sign in to comment.