Jonathan LaCour wrote:
> Alberto Valverde wrote:
>
>   
>> I think this could be solved in two ways which would allow default
>> rules to be bundled (after all, TG2 is all about providing sane
>> defaults to make simple things simple, right?)
>>     
>
> TG2 is about making simple things simple, but its not about doing it
> at the expense of complex things. Bottom line: both of your options
> make it much more difficult to write a simple custom jsonifier, and
> don't really solve the problem in a simple way.
>   
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)
> I want to have this at the top of my `json.py`:
>
> @jsonify.when((Person,))
> def jsonify_person(obj):
>      ...
>
> This is much simpler to write, and is way easier to explain to a new  
> user
> than something like this:
>
> @jsonify.when("isinstance(obj, Person) and crazy_disambiguator(1001)")
> def jsonify_person(obj):
>      ...*
>   

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...). Anyway, the "crazy_disambiguator" is just a simple
optional argument to the decorators, ie:

@jsonify.when((Person,), 1)
------------------------------------------^
> Imagine having to write this 50-60 times (in my case)!
>   
The following command could save you some time ;)

$ find . -name '*.py' -exec sed -e -i 's/\(@jsonify.*\))$/\1, 1)/g' {} ';'
> New users don't want to think about things like "generic function
> disambiguation". 
New users don't need to understand how the sausage is made in order to
eat it, I don't get your point.
> I think your ideas are both fine ideas, but they
> don't eliminate the need to be able to turn the defaults off.
> I think the simplest approach here is the best: put the defaults in
> a separate module, and have them be imported by default in the
> TG2 json.py template in your project. If you want to remove the
> defaults, you can simply delete that line from your `json.py` and
> you're set!
>   
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.

What if a some tg extension in the future needs to add rules to jsonify
their objects so they can be reused by user code? Now those rules are
disabled too, need to copy them again... suddenly all the power that
generic functions give framework developers to add hooks so their code
can be easily extended is lost.

Both solutions I proposed allow to *selectively* extend the encoder
while preserving rules any extension might have added or might need to add.
> The simplest solution is the best, IMO :)
>   
I fully agree, but I believe I've excelled in my previous at making them
sound much harder than they really are  ;)

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

Reply via email to