def default(self, obj): try: return json.JSONEncoder.default(self, obj) except TypeError, catched: if isinstance(obj, GeneratorType): return list(obj) if isinstance(obj, ResultsWrapper): return list(obj) elif isinstance(obj, datetime): return datetime_to_rfc3339(obj) raise catched
def decodeBSONEntity(di, key): """ Inspired by pymongo bson.json_util.default, but specially processing some value types: ObjectId --> hexvalue datetime --> rfc3339 """ value = di[key] if isinstance(value, ObjectId): di[key] = str(value) elif isinstance(value, datetime): di[key] = datetime_to_rfc3339(value)