Jonathan LaCour wrote:
> Alberto Valverde wrote:
>
>
>> Option 2 involves subclassing TJs encoder, in what way isn't that
>> simple? (and it might solve another bug for free as Chris Z has
>> pointed out)
>>
>
> Maybe I misunderstood, would rules still be able to be functions,
> rather than methods? I was under the impression that this method
> would involve changing the way that jsonification rules are
> written. If that isn't the case, then my objections to this method
> are completely withdrawn :)
>
Not at all, no need to change how they are written, the only thing that
changes is what jsonify is (an instance method instead of a function),
TJ could do this interanally so the API remains the same:
jsonify = GenericEncode.jsonify.im_func
>
>> Option 1 involves *no* change in user code since TJ's
>> implementations can have a lower priority than the default so any
>> extension made by the user automatically overrides the built ins
>> (I thought I had emphasized this point...).
>>
>
> Yeah, in my exhausted stupor, I must have missed that detail :) As
> long as the user's rules *always* override the defaults, I am a
> happy man! Objection withdrawn to this solution.
>
>
>> The following command could save you some time ;)
>>
>> $ find . -name '*.py' -exec sed -e -i 's/\(@jsonify.*\))$/\1,
>> 1)/g' {} ';'
>>
>
> Ah, sed! Incidentally, this is exactly what I would have done :)
>
I knew that, just wanted to counter your poor argument :P
>
>> So if you only need to override how Person is jsonified you
>> suddenly need to copy and paste (or re-implement for the fun of
>> it) the rule to jsonify all other SA mapped objects from tg since
>> you turned it turned it off en masse.
>>
>
> Well, this is what I was doing, because I actually don't want every
> single field of my SQLAlchemy objects to be serialized. I want to be
> able to explicitly control it, which is why I don't like there being
> no way to turn off the defaults! Having complete control over the
> JSONification of my objects is extremely important to me.
>
With the current implementation you can easily pop fields like this:
@jsonify.around("instantance(obj, Person)")
def jsonify_person(next_method, obj):
dct = next_method(obj) # call TJ's implementation
del dct['swiss_bank_acc_no']
return dct
>
>> Both solutions I proposed allow to *selectively* extend the
>> encoder while preserving rules any extension might have added or
>> might need to add.
>>
>
> Sure, but I still want to be able to turn them off, even if it
> does break some "future" extension. The bottom line for me is that
> I totally understand (and agree with) your arguments, but I still
> think that there needs to be a mechanism for clearing out the
> default rules to give the user complete control over them.
>
> Thanks for your hard work, Alberto!
>
Hard work? seems you were spying on me... ;) here's option 1:
http://toscawidgets.org/trac/rum/browser/rum/prioritized_methods.py
With tests and docs (for a change ;) You can use the prioritized_when
decorator
in there *now* to fix your TJ troubles until TJ adopts a definitive
solution (I'll probably release it as a standalone egg when it gets more
eyeballs).
from turbjson.jsonify import jsonify
from prioritized_methods import prioritized_when
jsonify.when = prioritized_when.__get__(jsonify)
@jsonify.when("isinstance(obj, Person"), prio=1)
def jsonify_person(obj):
....
(don't actually need the prio argument to override ATM since
prioritized_when overrides peak.rules.when by default)
Alberto
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears Trunk" 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-trunk?hl=en
-~----------~----~----~----~------~----~------~--~---