[web2py] Re: the way to return json
On Monday, September 18, 2017 at 9:59:32 PM UTC-4, 黄祥 wrote: > > a, i c, not tested on that yet, will do, btw, > forgot to add service.json() not sure about the difference or performance > compare with json/simplejson, as_json(), response.json(), restful > Again, ultimately, it just calls response.json. Anthony -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: the way to return json
a, i c, not tested on that yet, will do, btw, forgot to add service.json() not sure about the difference or performance compare with json/simplejson, as_json(), response.json(), restful e.g. @service.json def service_json_call_json_rows(): query = (db.auth_user.id > 0) rows = db(query).select() return rows lesson learned from tested above (json/simplejson, response.json(), as_json(), @request.restful(), @service.json) 1. when passing parameter to the function that generate json, using @service.json() and @request.restful() you can put the parameter in the function e.g. @service.json def get_table(table_name, id): or @request.restful() def api(): response.view = 'generic.' + request.extension def GET(*args, **vars): while in the other (json/simplejson, response.json() ) you must explicit it using e.g. id = request.args(0) 2. because of the reason above so that the parameter that you pass when using function (json/simplejson, response.json() ) couldn't have the extension .json on the url it will throw an error : ValueError: invalid literal for long() with base 10: 1.json 1 is a parameter args to do a query in table 3. if you have to post (insert), put (update), delete use @request.restful(), not sure if there any method beside @request.restful() 4. to create 1 function that support another format is using @service, you can change the result according to the service you choose (json, xml, etc) 5. the simple result is using as_json(), if the data in the table is same what you want to represent, if not, perhaps use another (json/simplejson, response.json() with for loop and then set the value data representation) e.g. def json_test(): query = (db.account.id > 0) rows = db(query).select() rows_list = [] for row in rows: rows_list.append([row.product.name, row.quantity] ) return response.json(rows_list) thanks and best regards, stifan -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: the way to return json
On Monday, September 18, 2017 at 9:09:59 AM UTC-4, 黄祥 wrote: > > yes you are right .as_list() will be called automatically when pass it to > response.json() > > rows.as_json() work well too unfortunately it's not documented in book yet: > def json_rows_as_json(): > query = (db.auth_user.id > 0) > rows = db(query).select() > return rows.as_json() > > *not sure about :* > @request.restful, you don't even have to call response.json -- if you make > an 'application/json' request, the result will automatically be converted > to JSON. > *code:* > @request.restful() > def restful_json_rows_as_list(): > response.view = 'generic.json' > def GET(): > query = (db.auth_user.id > 0) > rows = db(query).select() > return rows > return locals() > > *url:* > http://127.0.0.1:8000/test/api/restful_json_rows_as_list.json > As noted, you must request 'application/json', for example: curl -H "Accept: application/json" Anthony -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: the way to return json
yes you are right .as_list() will be called automatically when pass it to response.json() rows.as_json() work well too unfortunately it's not documented in book yet: def json_rows_as_json(): query = (db.auth_user.id > 0) rows = db(query).select() return rows.as_json() *not sure about :* @request.restful, you don't even have to call response.json -- if you make an 'application/json' request, the result will automatically be converted to JSON. *code:* @request.restful() def restful_json_rows_as_list(): response.view = 'generic.json' def GET(): query = (db.auth_user.id > 0) rows = db(query).select() return rows return locals() *url:* http://127.0.0.1:8000/test/api/restful_json_rows_as_list.json *result :* ... different result with code that return json format: @request.restful() def restful_json_rows_as_list(): def GET(): query = (db.auth_user.id > 0) rows = db(query).select() return response.json(rows) return locals() thanks and best regards, stifan -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: the way to return json
gluon.contrib.simplejson is not really simplejson but just a dummy module that calls the Python json module, so no difference between the first and second. I suspect response.json will be similar to json.dumps. response.json is gluon.serializers.json, which ultimately calls json.dumps, but it provides a custom "default" function to process objects that cannot otherwise be serialized (e.g., it handles dates, times, Rows objects, HTML helper objects, T() objects, etc.). Also, note, you don't even have to call rows.as_list() before passing to response.json, as .as_list() will be called automatically. In fact, with @request.restful, you don't even have to call response.json -- if you make an 'application/json' request, the result will automatically be converted to JSON. As for @request.restful, it may be a bit slower because the decorator involves some additional logic, but most of the time will probably be spent on the select and the serialization anyway. You can also try rows.as_json() just to keep things simple, but that's probably similar to response.json(rows) or response.json(rows.as_list()), as they all ultimately call the same methods to do the serializing (i.e., row.as_dict, gluon.serializers.json, and json.dumps). And of course, if you really want to know, you should just test it. Anthony On Sunday, September 17, 2017 at 7:42:50 PM UTC-4, 黄祥 wrote: > > just testing several code that return json > *e.g.* > > *controllers/api.py* > def json_rows_as_list(): > import json > query = (db.auth_user.id > 0) > rows = db(query).select() > json_list = json.dumps(rows.as_list(), default = str, sort_keys = True) > return dict(results = XML(json_list) ) > > """ > curl -X GET --user admin:password -i > http://127.0.0.1:8000/test/api/json_rows_as_list > curl -X GET --user admin:password -i > http://127.0.0.1:8000/test/api/json_rows_as_list.json > """ > > def simplejson_rows_as_list(): > import gluon.contrib.simplejson > query = (db.auth_user.id > 0) > rows = db(query).select() > json_list = gluon.contrib.simplejson.dumps(rows.as_list(), default = str, > sort_keys = True) > return dict(results = XML(json_list) ) > > """ > curl -X GET --user admin:password -i > http://127.0.0.1:8000/test/api/simplejson_rows_as_list > curl -X GET --user admin:password -i > http://127.0.0.1:8000/test/api/simplejson_rows_as_list.json > """ > > def response_json_rows_as_list(): > query = (db.auth_user.id > 0) > rows = db(query).select() > return response.json(rows.as_list() ) > > """ > curl -X GET --user admin:password -i > http://127.0.0.1:8000/test/api/response_json_rows_as_list > curl -X GET --user admin:password -i > http://127.0.0.1:8000/test/api/response_json_rows_as_list.json > """ > > @request.restful() > def restful_json_rows_as_list(): > def GET(): > query = (db.auth_user.id > 0) > rows = db(query).select() > return response.json(rows.as_list() ) > return locals() > > """ > curl -X GET --user admin:password -i > http://127.0.0.1:8000/test/api/restful_json_rows_as_list > curl -X GET --user admin:password -i > http://127.0.0.1:8000/test/api/restful_json_rows_as_list.json > """ > > is there any different in performance or anything between the way to > return json ? > > thanks and best regards, > stifan > -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.