Skip to content

Commit

Permalink
support insert bytes without bson
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlatwe committed Jun 7, 2021
1 parent a788e8b commit d477e3e
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions montydb/types/_bson.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import types
import base64


class BSON_(types.ModuleType):
Expand Down Expand Up @@ -204,6 +205,10 @@ def default(self, obj):
if isinstance(obj, NoBSON.ObjectId):
return {"$oid": str(obj)}

if isinstance(obj, bytes):
return {"$binary": {"base64": base64.b64encode(obj).decode(),
"subType": "00"}}

if isinstance(obj, NoBSON.datetime.datetime):
millis = NoBSON._datetime_to_millis(obj)
return {"$date": millis}
Expand Down Expand Up @@ -252,6 +257,8 @@ def document_encode(cls, doc, check_keys=False, *args, **kwargs):
def object_hook(cls, obj, opts=DEFAULT_CODEC_OPTIONS):
if "$oid" in obj:
return cls.ObjectId(obj["$oid"])
if "$binary" in obj:
return base64.b64decode(obj["$binary"]["base64"])
if "$date" in obj:
return cls._millis_to_datetime(int(obj["$date"]), opts)
if "$regex" in obj:
Expand Down

0 comments on commit d477e3e

Please sign in to comment.