-
-
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 json_response funciton #592
Comments
Any preference as to if/how the
try:
from simplejson as json # or anyjson? ujson?
except ImportError:
import json
import json
class JSONResponse(Response):
json_module = json
#... |
I think constructor parameter Another pull request may try to solve global configuration problem. See discussion for #336 also. |
Any reason not to pass in the Hypothetical use case: class JSONResponse(Response):
@classmethod
def from_json(cls, json_string: str, **kwargs):
return cls(data=self.json_module.loads(json_string), **kwargs) |
I don't see use case for deserialization of response. Different libraries may have different functions for unpacking data but I pretty sure all of them provides callable accepting dict and returning string. |
Hi, If you could do something like that: https://github.com/Eyepea/API-Hour/blob/master/api_hour/plugins/aiohttp/__init__.py It means we'll can remove this file: less code in API-Hour, better is ;-) We use a lot this helper. In this file, we have also an helper to output HTML files, be my guest to do something similar. |
I'm not reluctant for adding |
@asvetlov |
@jashandeep-sohi I believe Advanced user may provide serializer is needed. |
I only note, that content-type should be easily customized and not just as constant value, but with the respect of Accept header. Cases:
These details makes life a bit complicated, but not too much. |
Agree. It's also quite awkward that JsonResponse doesn't know how to transform data into JSON. |
@jashandeep-sohi basically, all these *Response is about to get some data, pass it through render function and form HTTPResponse with proper related metadata (status, headers, payload etc.). For JsonResponse this renderer is json.dump or whatever, for HTML one is jinja.render or special bridge to php or whatever. So both objects shares similar protocol and workflow. What you only need is to be able customize their behavior and be sure to not violate it. |
Okay, so |
@jashandeep-sohi I take you proposal for adding But yes, we can relax parameter list a bit to make both Anyway, I feel we need Pull Request for further discussion. Volunteers? |
Sorry, something must be getting lost in translation/typing, but I wasn't joking. I was genuinely asking how the |
@jashandeep-sohi What |
Okay, understood. |
Hi, guys! def json_response(data):
resp = web.Response()
resp.text = json.dumps(data)
resp.content_type = 'application/json'
return resp |
@popravich I feel no big difference between function and class, but class approach looks a bit better. |
My consern is that another class can evolve into something completle different from its parent, ie new mehods/attribues appear or else... Adding another type of response it just like confusing me as a user, like: But this all is just my conserns I just needed to say it aloud) |
The same issue appears with HTTP exceptions, right? You may use |
Well, not exactly) You just named it -- those are exceptions so having separate classes works fine for me. |
fixed by #599 |
If the functional way is the way to go, can we move
|
Class methods is too broad. |
Yeah, I forgot about inheritance. I know
vs
Doesn't matter really, it's up to you. |
But I don't like to have |
Yea, that is an ugly side effect.
`json_response` is fine then.
|
So, why not to have return web.Response(json={'answer_of_the_life': 42}) ? It is straighforward, easy and can not be misunderstood (as in Tornado, where it understand only dicts as json data). And also not require to use auxilary function like |
Well, as much as I like json and I use it a lot. I also hate having to use BeutifulSoup and lxml just to convert simple xml responces to json data. It ais also pain as well as I wanted to use the xml.dom.minidom in the standard library for the conversion of it to json. Hopefully an future version of aiohttp would allow me to convert xml responces to json so I can kill two dependencies with one stone (in this case aiohttp doing it for me). I can use json on request on most sites but some sites respond with xml or html even if you request to them with json. @socketpair do you agree we need some sort of conversion from xml to json in aiohttp that uses the standard library json and standard library xml to convert the responses to json when it is in xml (not html) form? |
Regarding XML question, it sounds like someone takes heavy drugs. So, I think we MUST NOT do such things in aiohttp. |
@AraHaan does stdlib has any analogue of |
|
Also, client API now has the same problem — |
Great points, @popravich. |
Maybe we should add Application-scope configuration, that allows to specify json (de-)serialiser globally ? I think real-life applications use single way of (de-)serialisation across all requests and responses. The rare cases when same objects should be (de-)serialised in different way are covered with |
Wait what @kxepal is |
I'm afraid this may not work well — real-life applications may need to handle several versions of self, |
...anyway, this all can be easily achieved with serialization middleware without need to modify aiohttp. |
Serialization makes total sens. But I don't like middleware idea. I understand it is right solution, but I prefer ergonomics of the framework. |
Should be derived from
aiohttp.web.Response
.Constructor signature is:
def __init__(self, data, *, status=200, reason=None, headers=None)
Should pack
data
arg asjson.dumps()
and set content type toapplication/json
.People forget to specify proper content type on sending json data.
The text was updated successfully, but these errors were encountered: