Skip to content
Browse files

* Add ability for jsonify to handle objects with a __json__ attribute…

… using

  custom JSONEncoder class similar to TG2. Patch by Bob Farrell.

--HG--
branch : trunk
  • Loading branch information...
1 parent 89a00d9 commit 548c0774750db18a6235ce556840ea9129645767 @bbangert bbangert committed
Showing with 11 additions and 1 deletion.
  1. +2 −0 CHANGELOG
  2. +9 −1 pylons/decorators/__init__.py
View
2 CHANGELOG
@@ -2,6 +2,8 @@ Pylons Changelog
================
1.1 (**tip**)
+* Add ability for jsonify to handle objects with a __json__ attribute using
+ custom JSONEncoder class similar to TG2. Patch by Bob Farrell.
* Added ability for __before__ to reference a callable function. Patch
contributed by mverdone.
* Pulled in JSON-RPC support from agentultra's pylons fork.
View
10 pylons/decorators/__init__.py
@@ -23,6 +23,14 @@
log = logging.getLogger(__name__)
+class JSONEncoder(simplejson.JSONEncoder):
+ def default(self, obj):
+ encoder = getattr(obj, '__json__', None)
+ if encoder is not None:
+ return encoder()
+ return super(JSONEncoder, self).default(obj)
+
+
@decorator
def jsonify(func, *args, **kwargs):
"""Action decorator that formats output for JSON
@@ -42,7 +50,7 @@ def jsonify(func, *args, **kwargs):
warnings.warn(msg, Warning, 2)
log.warning(msg)
log.debug("Returning JSON wrapped action output")
- return simplejson.dumps(data)
+ return simplejson.dumps(data, cls=JSONEncoder)
def validate(schema=None, validators=None, form=None, variable_decode=False,

0 comments on commit 548c077

Please sign in to comment.
Something went wrong with that request. Please try again.