Created
February 11, 2010 15:18
-
-
Save 1stvamp/301612 to your computer and use it in GitHub Desktop.
Pylons-esque jsonify decorator for django views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core import serializers | |
from django.http import HttpResponse | |
from django.utils import simplejson | |
from django.db.models import Model | |
def jsonify(fn, *args, **kwargs): | |
""" Decorator that serializes the output of a function, most likely | |
a view, as JSON, and returns the JSON in a HttpResponse. | |
Inspired by Pylon's jsonify controller decorator. | |
""" | |
def wrapper(*args, **kwargs): | |
output = fn(*args, **kwargs) | |
try: | |
value = simplejson.dumps(output) | |
except TypeError: | |
json_serializer = serializers.get_serializer('json') | |
serializer = json_serializer() | |
try: | |
serializer.serialize(output) | |
value = serializer.getvalue() | |
except TypeError, e: | |
# Try using jsonpickle if available | |
try: | |
import jsonpickle | |
value = jsonpickle.encode(output) | |
except ImportError: | |
raise e | |
return HttpResponse(value) | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment