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:

    1. >>> from bson.codec_options import DEFAULT_CODEC_OPTIONS
    2. >>> DEFAULT_CODEC_OPTIONS.tz_aware
    3. False
    4. >>> options = DEFAULT_CODEC_OPTIONS.with_options(tz_aware=True)
    5. >>> options.tz_aware
    6. 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 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 transform_bson method 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_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 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.

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.

TypeRegistry can be initialized with an iterable of type codecs, and a callable for the fallback encoder:

  1. >>> from bson.codec_options import TypeRegistry
  2. >>> type_registry = TypeRegistry([Codec1, Codec2, Codec3, ...],
  3. ... fallback_encoder)

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 bson type.

    • fallback_encoder (optional): callable that accepts a single, unencodable python value and transforms it into a type that bson can encode. See The fallback_encoder Callable documentation for an example.