> Doesn't seem to work..the encoding to json happens whenever I call it
> internally....
>
> Any other ideas?
>
>> > This is under TG 1.08.
>>
>> > I have an exposed function --using expose ()-- that I call using ajax,
>> > and it returns json.
>>
>> > So far so good.
>>
>> > I'd also like to call this function internally from another function,
>> > and just get back a regular python dictionary, not a json string. How
>> > can I do that?
>>
>> > I know there are a bunch of workarounds:
>> > - Making an exposed wrapper and separating it into two functions
>> > - "Loads"ing the json
>> > - etc.
>>
>> > but they all seem pretty inelegant.
>>
>> > Thanks
>>
>> @expose()
>> def get_stuff(self, cod_prov=None, format=None):
>> res = {} #something
>> if format == 'json':
>> from turbojson.jsonify import encode
>> return encode( res )
>> else:
>> return res
I'd say if you need this feature at all (calling an exposed method
sometimes internally, sometimes externally) your design could be
improved. When I needed something like this I realized that it's much
easier to have dedicated methods for @expose( ) that will only be
called from the outside and dedicated methods without @expose( ) that
will only be called internally. Reason being that this simplifies your
code and you avoid the kinds of hacks you might be tempted to do if
you insist on only one method.
So I always do things like this:
#############################################
def _method( self ):
# do the work
return dict( ..... )
@expose( )
def method( self ):
return self._method( )
#############################################
And so always call _method( ) internally, and method( ) only talks to
the outside world.
HTH,
Daniel
--
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---