-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add JSONResponse #599
Add JSONResponse #599
Conversation
closes aio-libs#592
@@ -810,3 +812,20 @@ def write_eof(self): | |||
if body is not None: | |||
self.write(body) | |||
yield from super().write_eof() | |||
|
|||
|
|||
def json_dumps(data): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest
class JSONResponse(Response):
def __init__(self, data, *, body=None, status=200,
reason=None, headers=None, content_type='application/json',
dumps=json.dumps):
super().__init__(text=text, status=status, reason=reason,
content_type=content_type)
My former comment was wrong.
|
Also, pass `text` rather than `body`
Changes made. Let me know if further changes are necessary or if I should squash the commits. |
What should happen if none of The current behavior is not ideal: JSONResponse() # TypeError: <object object at 0x10062e400> is not JSON serializable I can raise an error: if not any([data is not sentinel, text, body]):
raise ValueError(
'one of data, text, or body must be specified'
) but I'm not sure if we want to allow setting any of those attributes after a if data is not sentinel:
text = dumps(data)
super()... |
I've merged but replaced |
closes #592
Let me know if any further changes need to be made.