@@ -903,7 +903,7 @@ def _millis_to_datetime(millis, opts):
903903 micros = diff * 1000
904904 if opts .tz_aware :
905905 dt = EPOCH_AWARE + datetime .timedelta (seconds = seconds ,
906- microseconds = micros )
906+ microseconds = micros )
907907 if opts .tzinfo :
908908 dt = dt .astimezone (opts .tzinfo )
909909 return dt
@@ -924,6 +924,65 @@ def _datetime_to_millis(dtm):
924924 "codec_options must be an instance of CodecOptions" )
925925
926926
927+ def encode (document , check_keys = False , codec_options = DEFAULT_CODEC_OPTIONS ):
928+ """Encode a document to BSON.
929+
930+ A document can be any mapping type (like :class:`dict`).
931+
932+ Raises :class:`TypeError` if `document` is not a mapping type,
933+ or contains keys that are not instances of
934+ :class:`basestring` (:class:`str` in python 3). Raises
935+ :class:`~bson.errors.InvalidDocument` if `document` cannot be
936+ converted to :class:`BSON`.
937+
938+ :Parameters:
939+ - `document`: mapping type representing a document
940+ - `check_keys` (optional): check if keys start with '$' or
941+ contain '.', raising :class:`~bson.errors.InvalidDocument` in
942+ either case
943+ - `codec_options` (optional): An instance of
944+ :class:`~bson.codec_options.CodecOptions`.
945+
946+ .. versionadded:: 3.9
947+ """
948+ if not isinstance (codec_options , CodecOptions ):
949+ raise _CODEC_OPTIONS_TYPE_ERROR
950+
951+ return _dict_to_bson (document , check_keys , codec_options )
952+
953+
954+ def decode (data , codec_options = DEFAULT_CODEC_OPTIONS ):
955+ """Decode BSON to a document.
956+
957+ By default, returns a BSON document represented as a Python
958+ :class:`dict`. To use a different :class:`MutableMapping` class,
959+ configure a :class:`~bson.codec_options.CodecOptions`::
960+
961+ >>> import collections # From Python standard library.
962+ >>> import bson
963+ >>> from bson.codec_options import CodecOptions
964+ >>> data = bson.encode({'a': 1})
965+ >>> decoded_doc = bson.decode(data)
966+ <type 'dict'>
967+ >>> options = CodecOptions(document_class=collections.OrderedDict)
968+ >>> decoded_doc = bson.decode(data, codec_options=options)
969+ >>> type(decoded_doc)
970+ <class 'collections.OrderedDict'>
971+
972+ :Parameters:
973+ - `data`: the BSON to decode. Any bytes-like object that implements
974+ the buffer protocol.
975+ - `codec_options` (optional): An instance of
976+ :class:`~bson.codec_options.CodecOptions`.
977+
978+ .. versionadded:: 3.9
979+ """
980+ if not isinstance (codec_options , CodecOptions ):
981+ raise _CODEC_OPTIONS_TYPE_ERROR
982+
983+ return _bson_to_dict (data , codec_options )
984+
985+
927986def decode_all (data , codec_options = DEFAULT_CODEC_OPTIONS ):
928987 """Decode BSON data to multiple documents.
929988
@@ -935,7 +994,7 @@ def decode_all(data, codec_options=DEFAULT_CODEC_OPTIONS):
935994 - `codec_options` (optional): An instance of
936995 :class:`~bson.codec_options.CodecOptions`.
937996
938- .. versionchanges :: 3.9
997+ .. versionchanged :: 3.9
939998 Supports bytes-like objects that implement the buffer protocol.
940999
9411000 .. versionchanged:: 3.0
@@ -1137,6 +1196,10 @@ def is_valid(bson):
11371196
11381197class BSON (bytes ):
11391198 """BSON (Binary JSON) data.
1199+
1200+ .. warning:: Using this class to encode and decode BSON adds a performance
1201+ cost. For better performance use the module level functions
1202+ :func:`encode` and :func:`decode` instead.
11401203 """
11411204
11421205 @classmethod
@@ -1163,10 +1226,7 @@ def encode(cls, document, check_keys=False,
11631226 .. versionchanged:: 3.0
11641227 Replaced `uuid_subtype` option with `codec_options`.
11651228 """
1166- if not isinstance (codec_options , CodecOptions ):
1167- raise _CODEC_OPTIONS_TYPE_ERROR
1168-
1169- return cls (_dict_to_bson (document , check_keys , codec_options ))
1229+ return cls (encode (document , check_keys , codec_options ))
11701230
11711231 def decode (self , codec_options = DEFAULT_CODEC_OPTIONS ):
11721232 """Decode this BSON data.
@@ -1208,10 +1268,7 @@ def decode(self, codec_options=DEFAULT_CODEC_OPTIONS):
12081268
12091269 .. _PYTHON-500: https://jira.mongodb.org/browse/PYTHON-500
12101270 """
1211- if not isinstance (codec_options , CodecOptions ):
1212- raise _CODEC_OPTIONS_TYPE_ERROR
1213-
1214- return _bson_to_dict (self , codec_options )
1271+ return decode (self , codec_options )
12151272
12161273
12171274def has_c ():
0 commit comments