On Tuesday, July 30, 2013 11:51:08 AM UTC-4, Jonathan Lundell wrote:
>
> On 30 Jul 2013, at 8:34 AM, Anthony <abas...@gmail.com <javascript:>> 
> wrote:
>
> Can you show your code? You say you "return locals()", but locals() 
> produces a dictionary, so it should ultimately execute a view. Or are you 
> just returning the list directly?
>
>
>
Here is a snippit of what we are trying to do:



import simplejson 

@request.restful() 
def jsonlisttest():
response.view = 'generic.json' 

def GET(*args, **vars): 

sample_raw_rpc_response = '["one", "two", "three"]' 

sample_rpc_response = simplejson.loads(sample_raw_rpc_response) 

return sample_rpc_response 

def POST(*args, **vars): 

return dict() 

def PUT(*args, **vars): 

return dict() 

def DELETE(*args, **vars): 

return dict() 

return locals()

 

> A list directly (see the code fragment below). 
>
> Since generic.json serializes response._vars, it doesn't care about 
> anything in env. You *could* write a view that accepts a dict with a list 
> (or dict) as a well-known name and serialize that, but it's not what 
> generic.json does.
>
>
> Anthony
>
> On Tuesday, July 30, 2013 10:28:53 AM UTC-4, Matt wrote:
>>
>>
>>
>> On Tuesday, July 30, 2013 10:26:45 AM UTC-4, Jonathan Lundell wrote:
>>>
>>> On 30 Jul 2013, at 12:22 AM, Niphlod <nip...@gmail.com> 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.
>>>
>>>
>>> The explanation isn't quite clear. I believe he's returning a list, 
>>> intending that to be serialized as JSON. But web2py serializes anything 
>>> that's not a dict as a string, so generic.json (for example) is never used.
>>>
>>> The question is: what should happen when a controller returns a list? 
>>> The JSON serializer is happy to serialize a list. Is there any downside in 
>>> doing it? Does it make any sense right now for a controller to return a 
>>> list?
>>>
>>>
>> Exactly. Apologies for lack of clarity.
>>
>> Matt
>>
>>>
>>> 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