Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #2409 (closed enhancement: fixed)

Opened 10 years ago

Last modified 9 years ago

Bring Back Generic Function JSON, Optionally

Reported by: cleverdevil Owned by: jorge.vargas
Priority: normal Milestone: 2.1b1
Component: TurboGears Version: trunk
Severity: normal Keywords: json


Attached is a very simple patch which brings back the ability to define JSON rules externally using generic functions. Rather than being based upon PEAK-Rules, this code is based upon the much more documented and easy_installable simplegeneric, which at one point was considered for inclusion in the standard library. Unlike PEAK-Rules, simplegeneric only allows you to define very simple rules based upon types, rather than arbitrary rules with multiple conditions.

As you'll notice, the patch doesn't force people to install simplegeneric or use generic functions if they do not want to. The code falls back to the old behavior based upon the presence of simplegeneric.


generic.diff Download (688 bytes) - added by cleverdevil 10 years ago.
test_jsonify_generic.py Download (2.2 KB) - added by cleverdevil 10 years ago.

Change History

Changed 10 years ago by cleverdevil

comment:1 Changed 10 years ago by mramm

Looks reasonable to me. +1

Would be +10 if it came with tests that proved that it worked. ;)

comment:2 Changed 10 years ago by cleverdevil

I have attached some unit tests to make Mark approximately +9 happier :)

Changed 10 years ago by cleverdevil

comment:3 Changed 10 years ago by percious

im -1 on this.

I think we should add a new renderer that supports this, like 'json_generic' or something.

comment:4 Changed 10 years ago by cleverdevil

Any particular reason for you to be -1? Unless you have a patch which provides the same functionality in a demonstrably better way, I say its time to put this in. The code is simple, short, is totally optional, and brings back a capability that some people (including myself) relied upon in TG 1.x without introducing a whole new package or additional complexity. I've not heard a single good argument against it.

comment:5 Changed 10 years ago by mramm

I remain +10 on this. The feature is actually used not just by jonathan, and I think that the optional nature of simplegeneric extensibility makes this a no brainer in terms of backwards compatibility, and it provides an easy way to gather your rules on how to jsonify objects in a particular place.

comment:6 Changed 10 years ago by percious

What I don't like about it is the solution. I don't really like using import to determine functionality. I did this with tgext.admin and dojo and had to go back and re-engineer around it because it was too inflexible. I _like_ the idea of adding this functionality, but not in this way. This is why I suggested adding a new renderer called json_generic that would provide this functionality without stomping existing functionality, or using a conditional import. I'd hate to have to tell people to "uninstall" generics because there is no easy_uninstall.

comment:7 Changed 10 years ago by jorge.vargas

Sorry for being so late to this party. I have been to busy to take a good look at this.

I agree with mramm and cleverdevil that this patch is good enough. However I do agree with percious that "if it's installed use it" is not the best way to have an optional dependency.

That said the only other way I see we could implement this is with a flag in app_cfg to turn on generic functions for json, which I know percious dislikes even more. Therefore unless there is a concrete objection (with an alternative patch) I'm going to apply this next time I work on TG code.

comment:8 Changed 10 years ago by jorge.vargas

  • Owner set to jorge.vargas

comment:9 Changed 9 years ago by percious

  • Status changed from new to closed
  • Resolution set to fixed

comment:10 Changed 9 years ago by percious

  • Milestone changed from 2.1b2 to 2.1b1
Note: See TracTickets for help on using tickets.