Just to make this clear for future people looking at this.
On Mon, Jun 8, 2009 at 7:57 PM, Carl<[email protected]> wrote:
>
> Thanks, Christoph. This makes sense. I noticed that if you additional
> expose() decorators on the controller method then it is possible to
> return lists and strings even if the request type is JSON.
>
YOu can do it in either
a-
@expose('json')
def foo(self....):
return dict(...)
This will call TurboJson (2.0) an tg/json.py (2.1) and generate the
json data for you.
or
b-
@expose(content-type='text/json')
def foo(self....):
return simplejson.dumps() #this is a string
Please note there is a bug with form b (2.0) and you need to set the
content-type inside the method, it's explained in the 2.0 docs.
Both forms are valid and you could use one or the other depending on
your case. For A is nicer because it providers automatic serialization
for common types, for example SQLAlchemy's own internal types so you
can return DBsession.query(...) and it will know how to jsonify that.
And that is my first extract of my documentation bit on how to work
with json and TG, if someone wants to start it please go ahead :)
> Carl
>
> On Jun 8, 1:37 am, Christoph Zwerschke <[email protected]> wrote:
>> Carl schrieb:
>>
>> > I can't return a non-dictionary object (e.g. a list of dictionaries or
>> > a string) from a controller method with expose('json'). If I do
>> > directory simplejson.dumps() it works.
>>
>> I think this ishttp://trac.turbogears.org/ticket/2034
>>
>> -- Christoph
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---