Let JSONResponse accept a custom json encoder from standard json library #2990
-
|
I was using some validators and exception handlers while working on an API (using My script is something like this: If I run it then I get a I found a solution by constructing a custom JSON encoder that can JSON serialize the exceptions by passing it to the When looking specifically into the And, even if your current documentation states to subclass So, I think it might be much simpler/cleaner if we just add the support to the WDYT? Btw, I already have a PR with implementation of this idea if you want to take a look :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
From a user perspective, isn’t it simpler to subclass # your_module/responses.py
from starlette.responses import JSONResponse as BaseJSONResponse
class JSONResponse(BaseJSONResponse):
def render(self, content: Any) -> bytes:
…
# Then everywhere in your code:
from your_module.responses import JSONResponse |
Beta Was this translation helpful? Give feedback.
From a user perspective, isn’t it simpler to subclass
JSONResponseonce and reuse the class everywhere, rather than having to remember to import the custom encoder and pass it to every singlereturn JSONResponse()?