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.

Reply via email to