Created
August 6, 2013 11:45
-
-
Save andreagrandi/6163811 to your computer and use it in GitHub Desktop.
A more compact version of FbxEncoder for DjangoRestFramework.
This file contains hidden or 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 rest_framework import renderers | |
from rest_framework.utils.encoders import JSONEncoder | |
from bson import ObjectId | |
class FbxJSONEncoder(JSONEncoder): | |
""" | |
FbxJSONEncoder subclass that knows how to encode date/time/timedelta, | |
decimal types, generators and Mongo ObjectId. | |
""" | |
def default(self, o): | |
if isinstance(o, ObjectId): | |
return str(o) | |
return JSONEncoder.default(self, o) | |
class FbxRenderer(renderers.JSONRenderer): | |
encoder_class = FbxJSONEncoder |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment