Skip to content

Instantly share code, notes, and snippets.

@haoch
Created November 1, 2012 11:18
Show Gist options
  • Save haoch/3993101 to your computer and use it in GitHub Desktop.
Save haoch/3993101 to your computer and use it in GitHub Desktop.
django tasypie : custom serializer for fixing some bugs while serializing response on some platform like windows
from tastypie.serializers import Serializer
import json
class CustomJSONSerializer(Serializer):
""" Custom JSON Serializer
Replace the default simplejson.DjangoJSONEncoder in tastypie with json.JSONEncoder
USAGE: avoid json encoding error in django 1.5+
Author: cn.haochen@gmail.com """
#override super.to_json()
def to_json(self,data,options=None):
options = options or {}
data = self.to_simple(data,options)
return json.dumps(data,cls=json.JSONEncoder,sort_keys=True)
#override super.from_json()
def from_json(self,content):
data = json.loads(content)
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment