We've got a controller with a restful method (using the @request.restful 
decorator), which makes calls over a json rpc socket and then returns the 
result. Sometimes the result of the json rpc call is a list of strings, 
which is properly converted back into python as a list type. When we do 
"return locals()" at this point (a la the rest services example in the 
book), even with a forced request.extension = 'json' and response.view = 
'generic.json' the value is not rendered as json but instead as just a 
plain string.

My sense is because python is following the path in main.py that I 
mentioned previously, determining the result is not a dict, and therefore 
not running any of the views on it (hence generic.json is not being 
called/used). Further, its just passing the list of strings along and 
python is concatenating that into just a single string.

It's not ideal for us to manually convert the result to a string just for 
this case where everything else works just fine by making it into the 
generic.json view. This used to "just work" for us because we did manual 
json.dumps conversion of the data, but this is the whole point of 
generic.json right? 

Regards,
Matt


On Tuesday, July 30, 2013 3:22:54 AM UTC-4, Niphlod wrote:
>
> can you explain better what's going on ? the generic json view (as any 
> other view) is made to serialize a python object to something (in json's 
> case, a json string). 
> If you already return a string because your code encodes it already, than 
> you don't need any view, because the job the view usually does has been 
> done already by your code.
>
> On Monday, July 29, 2013 11:07:35 PM UTC+2, Matt wrote:
>>
>> Hi,
>> We're running into an issue with our restful api where a certain method 
>> is returning a json string (eg: " ['one', 'two', 'three'] "), however 
>> web2py wants to render this as a string even when the request.extension is 
>> forced to json, and the response.view is forced to generic.json. I've 
>> tracked the issue down to gluon/main.py:231 where it checks if "page" is a 
>> dict, and what seems to be happening is that the valid json string above is 
>> instead being concatenated as a string representation. Adding this below 
>> the check for dict seems to fix it, but I'm not sure how well it fixes the 
>> problem:
>>
>>     elif isinstance(page, list):
>>         response._vars = page
>>         run_view_in(response._view_environment)
>>         page = response.body.getvalue()
>>
>> Any suggestions for other ways to go about fixing this? 
>>
>> Thanks,
>> Matt
>>
>>
>>

-- 

--- 
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/groups/opt_out.


Reply via email to