Skip to content

Instantly share code, notes, and snippets.

@andreagrandi
Created August 6, 2013 11:45
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 andreagrandi/6163811 to your computer and use it in GitHub Desktop.
Save andreagrandi/6163811 to your computer and use it in GitHub Desktop.
A more compact version of FbxEncoder for DjangoRestFramework.
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