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

[Feature Request] Provide a JsonResponse class #689

Closed
sk- opened this issue Dec 17, 2015 · 4 comments
Closed

[Feature Request] Provide a JsonResponse class #689

sk- opened this issue Dec 17, 2015 · 4 comments

Comments

@sk-
Copy link

sk- commented Dec 17, 2015

We have been using aiohttp successfully, however we miss a simpler way of generating json responses.
For such we have a utility classes defining a JsonResponse class. Note that using directly a web.Response is tedious, as you have to set the content_type, and probably you want to convert to JSON at the very last minute.

Our (hacky) implementation is as follows:

class JSONResponse(aiohttp.web.Response):
    """Build an aiohttp.web.Response using a json object."""
    def __init__(self, *, data=None, status=200, reason=None, headers=None):
        super().__init__(content_type=JSON_CONTENT_TYPE, status=status,
                         reason=reason, headers=headers)
        self.data = data

    # The overrides below allow us to change the content of the data object and
    # lazily convert it to an UTF-8 encoded byte string.
    @property
    def body(self):
        body_content = json.dumps(self.data).encode('utf-8')
        self.content_length = len(body_content)
        return body_content

    @body.setter
    def body(self, value):
        pass

    @property
    def _body(self):
        return self.body

    @_body.setter
    def _body(self, value):
        pass

I would really love to have JSON support to be much simpler out of the box, and would be happy to contribute a non hacky version.

@asvetlov asvetlov modified the milestone: 0.20 Dec 17, 2015
@asvetlov
Copy link
Member

Use json_response instead.
See #592 for objections against class-based implementation.

@sk-
Copy link
Author

sk- commented Dec 17, 2015

I tried to use that, but one problem with that approach, is that you no longer have access to the original data. So for example, it is not possible to have a middleware that inspects the data and does something with it (my use case), or in tests for example.

Another problem is that nothing restricts you from changing the underlying text representation. So you could end up with a malformed json body.

Also, the data is serialized eagerly, instead of lazily. Which means that you would incur the cost of serializing even if you end up dropping that response.

@asvetlov
Copy link
Member

I got your point.
You want to use middleware for json modification.
Someone else might want to use them for image changing: resizing or saturation adjustment for example.

Both usages may be fine for application code but will never get a chance to land into aiohttp library.
If you really need JSONResponse class -- please keep it inside your code bounds.

@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants