Created
May 16, 2011 21:13
-
-
Save jduckles/975387 to your computer and use it in GitHub Desktop.
handler
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
# TODO support Binary and Code | |
import calendar | |
import datetime | |
import re | |
try: | |
import uuid | |
_use_uuid = True | |
except ImportError: | |
_use_uuid = False | |
from bson.dbref import DBRef | |
from bson.max_key import MaxKey | |
from bson.min_key import MinKey | |
from bson.objectid import ObjectId | |
from bson.timestamp import Timestamp | |
from bson.tz_util import utc | |
def handler(obj): | |
if isinstance(obj, ObjectId): | |
return {"$oid": str(obj)} | |
if isinstance(obj, DBRef): | |
return obj.as_doc() | |
if isinstance(obj, datetime.datetime): | |
# TODO share this code w/ bson.py? | |
return obj.isoformat() | |
if isinstance(obj, _RE_TYPE): | |
flags = "" | |
if obj.flags & re.IGNORECASE: | |
flags += "i" | |
if obj.flags & re.MULTILINE: | |
flags += "m" | |
return {"$regex": obj.pattern, | |
"$options": flags} | |
if isinstance(obj, MinKey): | |
return {"$minKey": 1} | |
if isinstance(obj, MaxKey): | |
return {"$maxKey": 1} | |
if isinstance(obj, Timestamp): | |
return {"t": obj.time, "i": obj.inc} | |
if _use_uuid and isinstance(obj, uuid.UUID): | |
return {"$uuid": obj.hex} | |
raise TypeError("%r is not JSON serializable" % obj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment