codec_options
– Tools for specifying BSON codec options
class bson.codec_options.CodecOptions(document_class=<class ‘dict’>, tz_aware=False, uuid_representation=None, unicode_decode_error_handler=’strict’, tzinfo=None, type_registry=None)
Create new instance of CodecOptions(document_class, tz_aware, uuid_representation, unicode_decode_error_handler, tzinfo, type_registry)
with_options(\*kwargs*)
Make a copy of this CodecOptions, overriding some options:
New in version 3.5.
class bson.codec_options.TypeCodec
Base class for defining type codec classes which describe how a custom type can be transformed to/from one of the types bson can already encode/decode.
Codec classes must implement the python_type
attribute, and the transform_python
method to support encoding, as well as the bson_type
attribute, and the method to support decoding.
See documentation for an example.
class bson.codec_options.TypeDecoder
Codec classes must implement the bson_type
attribute, and the transform_bson
method to support decoding.
See The TypeCodec Class documentation for an example.
abstract property bson_type
The BSON type to be converted into our own type.
abstract transform_bson(value)
Convert the given BSON value into our own type.
class bson.codec_options.TypeEncoder
Base class for defining type codec classes which describe how a custom type can be transformed to one of the types BSON understands.
Codec classes must implement the python_type
attribute, and the transform_python
method to support encoding.
See documentation for an example.
abstract transform_python(value)
Convert the given Python object into something serializable.
class bson.codec_options.TypeRegistry(type_codecs=None, fallback_encoder=None)
Encapsulates type codecs used in encoding and / or decoding BSON, as well as the fallback encoder. Type registries cannot be modified after instantiation.
can be initialized with an iterable of type codecs, and a callable for the fallback encoder:
>>> from bson.codec_options import TypeRegistry
>>> type_registry = TypeRegistry([Codec1, Codec2, Codec3, ...],
See The TypeRegistry Class documentation for an example.
Parameters
type_codecs (optional): iterable of type codec instances. If
type_codecs
contains multiple codecs that transform a single python or BSON type, the transformation specified by the type codec occurring last prevails. A TypeError will be raised if one or more type codecs modify the encoding behavior of a built-in type.fallback_encoder (optional): callable that accepts a single, unencodable python value and transforms it into a type that bson can encode. See documentation for an example.