codec_options – Tools for specifying BSON 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:
>>> from bson.codec_options import DEFAULT_CODEC_OPTIONS >>> DEFAULT_CODEC_OPTIONS.tz_aware False >>> options = DEFAULT_CODEC_OPTIONS.with_options(tz_aware=True) >>> options.tz_aware True
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
bsoncan already encode/decode.Codec classes must implement the
python_typeattribute, and thetransform_pythonmethod to support encoding, as well as thebson_typeattribute, and thetransform_bsonmethod to support decoding.See The TypeCodec Class documentation for an example.
-
class
bson.codec_options.TypeDecoder¶ Base class for defining type codec classes which describe how a BSON type can be transformed to a custom type.
Codec classes must implement the
bson_typeattribute, and thetransform_bsonmethod 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.
-
abstract property
-
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_typeattribute, and thetransform_pythonmethod to support encoding.See The TypeCodec Class documentation for an example.
-
abstract property
python_type¶ The Python type to be converted into something serializable.
-
abstract
transform_python(value)¶ Convert the given Python object into something serializable.
-
abstract property
-
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.
TypeRegistrycan 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, ...], ... fallback_encoder)
See The TypeRegistry Class documentation for an example.
- Parameters
type_codecs (optional): iterable of type codec instances. If
type_codecscontains 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-inbsontype.fallback_encoder (optional): callable that accepts a single, unencodable python value and transforms it into a type that
bsoncan encode. See The fallback_encoder Callable documentation for an example.