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.

Reply via email to