Skip to content

Instantly share code, notes, and snippets.

@jduckles
Created May 16, 2011 21:13
Show Gist options
  • Save jduckles/975387 to your computer and use it in GitHub Desktop.
Save jduckles/975387 to your computer and use it in GitHub Desktop.
handler
# 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