> On Sat, Sep 11, 2010 at 5:45 PM, Wyatt Baldwin
> <
wyatt.lee.bald...@gmail.com>wrote:
>
> > I'm not really sure what's going on there, but when you use
> > map.resource, you can do this instead of adding a special `json`
> > action:
>
> > GET /post/1.json
>
> > Then 'json' will be passed as the `format` arg to the appropriate REST
> > action. In this example:
>
> > def show(self, id, format='html'):
> > # id is '1' and format is 'json'
> > # ...
> > if format == 'json':
> > # convert member to json and return it
>
>
On Sep 11, 5:55 pm, Wojtek Augustynski <
waugustyn...@gmail.com> wrote:
> Thanks, for the response. So doing it that way (other then the decorator
> way) i just change the mime type and use simplejson.dumps?
Well, there are various approaches you could take, but I'd add another
method that specifically handles returning JSON responses:
def show(self, id, format='html'):
entity = Session.query(Entity).get(id)
if format == 'json':
return self._render_json(entity)
@jsonify
def _render_json(self, entity):
# I'd put this in a controller base class
# convert `entity` to some kind of object that can be
JSONified, probably a dict
return object_that_can_be_jsonified
Note, this just shows the general approach I would take. For example,
this doesn't deal with collections. If you want to see what I
*actually* do, look here and check out the various `_render` methods:
http://code.google.com/p/restler/source/browse/restler/controller.py