On Thu, Aug 13, 2009 at 1:28 PM, David Broudy<[email protected]> wrote:
>
> On Aug 13, 2009, at 2:12 AM, Jorge Vargas wrote:
>
> Your solution for this problem could work with the
> "when" decorator but I don't see it as the best solution. Which
> criteria will you use to strip all unwanted attributes from all
> objects? It seems to me like a very big set of hasattr check that will
> return false on all objects that don't have it and only true on a flew
> that do.
>
> Wouldn't it be better to subclass the serializer and do things from
> there? again which will be the criteria? Will anyone come up with an
> example where this is needed?
>
>
> The case I was working with was serializing the lists from an
> AssociationProxy. I'm pretty new to TG, but it seems to me the default
> behavior isn't great, at least in TurboJson 1.2.1, skipping keys starting
> with _sa_ but including the private _AssociationProxy_* collections, which
> aren't really in a usable format. I think it should either skip the private
> proxy collection too, or include it in a usable form. Undoubtedly skipping
> it makes more sense as a default behavior. Maybe this is really a SA issue,
> because not all of their private keys start with _sa_, but it seems easy
> enough to fix at the turbojson level.

Umm this is interesting AssociationProxy is an extension that doesn't
follow the _sa_ convention. And I agree this is a rather simple patch.
In fact it should be as simple as adding a new function to
http://svn.turbogears.org/projects/TurboJson/trunk/turbojson/jsonify.py
will you work on a patch + test for it? I don't have any models around
that use that extension.

> My model only had one class that used an association proxy, but if you had a
> bunch of classes that had a reasonably finite set of objects in an
> association proxy (say <10 keywords each), I think that's a use case that
> justifies a global way to do it for a given project, such as the when
> decorator. And if they were all called "keywords", then it's only one
> hasattr to check.
> I hadn't thought about subclassing the whole serializer. I think that's a little bit more intimidating, but like I said, I'm pretty new to TG, so I'm still trying to absorb it all.
> In my case, subclassing would have made sense, because I wanted to take out
> something (_AssociationProxy_* keys) and I couldn't figure out how to do
> that in a when
> decorator without basically overriding the whole process anyway.

In TurboJson this will be done with a simple jsonify.when decorated
function that will magically get called by TurboJson. As explained
here http://docs.turbogears.org/1.0/JsonifyDecorator

Now as I said that has been removed (although an equivalent
implementation could be set in place for 2.1, the question is do we
want it?) the proper way will be to subclass the Encoder here and (for
now monkey patch L39: _instance = GenericJSON()) to put in your
modified instance. Suggestions on how to make that better are welcome.

So in summary I believe subclassing GenericJSON + telling TG to use
your version is WAY simpler than having someone go try to understand
generic functions and then implement one.

Back to the specific case I really believe this should be patched into
tg2.1 and TurboJson as AssociationProxy needs to be filtered (at least
by default) just like every other SA relation.

>
> I think the decorator syntax is nice and clean, and the best examples are in
> TurboJson it self: datetime, decimal and explicit. Presumably those are
> handled in TG 2.1 as well, so hopefully there's a nice way to subclass and
> override similar types of objects, such as a duration or a currency.

yes indeed the functionality is 99% equivalent the only thing missing
is the "when", the problem is that the old implementation used generic
functions which well created a ton of dependencies and did not bring
in a lot. I know it sounds like a lot but pretty much all of TurboJson
was replaced by one 50 lines file
http://hg.turbogears.org/tg-21/src/tip/tg/jsonify.py which is
equivalent and I'm pretty sure it's faster.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to