Skip to content
Browse files

more json support - check for 'to_json' method

  • Loading branch information...
1 parent 16e465f commit a73c7248fe987785e853b904b2313d9a3dc7cbd3 Colin Thomas-Arnold committed Apr 4, 2012
Showing with 7 additions and 5 deletions.
  1. +7 −5 fusionbox/templatetags/fusionbox_tags.py
View
12 fusionbox/templatetags/fusionbox_tags.py
@@ -169,15 +169,17 @@ def attr(obj, arg1):
return obj
-def encode_decimal(d):
- if isinstance(d, Decimal):
- return float(d)
- raise TypeError("%r is not JSON serializable" % (d,))
+def more_json(obj):
+ if isinstance(obj, Decimal):
+ return float(obj)
+ if hasattr(obj, 'to_json'):
+ return obj.to_json()
+ raise TypeError("%r is not JSON serializable" % (obj,))
@register.filter
def json(a):
- return mark_safe(simplejson.dumps(a, default=encode_decimal))
+ return mark_safe(simplejson.dumps(a, default=more_json))
json.is_safe = True

0 comments on commit a73c724

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