Created
August 24, 2011 19:38
-
-
Save jefftriplett/1168982 to your computer and use it in GitHub Desktop.
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
| import datetime | |
| import pymongo | |
| import simplejson | |
| from pymongo.objectid import ObjectId | |
| class DateTimeMongoEncoder(simplejson.JSONEncoder): | |
| def default(self, obj): | |
| if isinstance(obj, (datetime.date, datetime.datetime)): | |
| return obj.strftime('%Y-%m-%dT%H:%M:%S') | |
| elif isinstance(obj, ObjectId): | |
| return """ObjectId("%s")""" % str(obj) | |
| else: | |
| return simplejson.JSONEncoder.default(self, obj) | |
| simplejson.dumps(item, cls=DateTimeMongoEncoder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment