@@ -903,7 +903,7 @@ def _millis_to_datetime(millis, opts):
903
903
micros = diff * 1000
904
904
if opts .tz_aware :
905
905
dt = EPOCH_AWARE + datetime .timedelta (seconds = seconds ,
906
- microseconds = micros )
906
+ microseconds = micros )
907
907
if opts .tzinfo :
908
908
dt = dt .astimezone (opts .tzinfo )
909
909
return dt
@@ -924,6 +924,65 @@ def _datetime_to_millis(dtm):
924
924
"codec_options must be an instance of CodecOptions" )
925
925
926
926
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
+
927
986
def decode_all (data , codec_options = DEFAULT_CODEC_OPTIONS ):
928
987
"""Decode BSON data to multiple documents.
929
988
@@ -935,7 +994,7 @@ def decode_all(data, codec_options=DEFAULT_CODEC_OPTIONS):
935
994
- `codec_options` (optional): An instance of
936
995
:class:`~bson.codec_options.CodecOptions`.
937
996
938
- .. versionchanges :: 3.9
997
+ .. versionchanged :: 3.9
939
998
Supports bytes-like objects that implement the buffer protocol.
940
999
941
1000
.. versionchanged:: 3.0
@@ -1137,6 +1196,10 @@ def is_valid(bson):
1137
1196
1138
1197
class BSON (bytes ):
1139
1198
"""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.
1140
1203
"""
1141
1204
1142
1205
@classmethod
@@ -1163,10 +1226,7 @@ def encode(cls, document, check_keys=False,
1163
1226
.. versionchanged:: 3.0
1164
1227
Replaced `uuid_subtype` option with `codec_options`.
1165
1228
"""
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 ))
1170
1230
1171
1231
def decode (self , codec_options = DEFAULT_CODEC_OPTIONS ):
1172
1232
"""Decode this BSON data.
@@ -1208,10 +1268,7 @@ def decode(self, codec_options=DEFAULT_CODEC_OPTIONS):
1208
1268
1209
1269
.. _PYTHON-500: https://jira.mongodb.org/browse/PYTHON-500
1210
1270
"""
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 )
1215
1272
1216
1273
1217
1274
def has_c ():
0 commit comments