Skip to content

Instantly share code, notes, and snippets.

@1stvamp
Created February 11, 2010 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1stvamp/301612 to your computer and use it in GitHub Desktop.
Save 1stvamp/301612 to your computer and use it in GitHub Desktop.
Pylons-esque jsonify decorator for django views
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