Created
November 1, 2012 11:18
-
-
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
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 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: [email protected] """ | |
#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