Encode a document to a new :class:`BSON` instance. A document can be any mapping type (like :class:`dict`). Raises :class:`TypeError` if `document` is not a mapping type, or contains keys that are not instances of :class:`str'. Raises :class:`~bson.errors.InvalidDoc
(
cls: Type[BSON],
document: Mapping[str, Any],
check_keys: bool = False,
codec_options: CodecOptions[Any] = DEFAULT_CODEC_OPTIONS,
)
| 1407 | |
| 1408 | @classmethod |
| 1409 | def encode( |
| 1410 | cls: Type[BSON], |
| 1411 | document: Mapping[str, Any], |
| 1412 | check_keys: bool = False, |
| 1413 | codec_options: CodecOptions[Any] = DEFAULT_CODEC_OPTIONS, |
| 1414 | ) -> BSON: |
| 1415 | """Encode a document to a new :class:`BSON` instance. |
| 1416 | |
| 1417 | A document can be any mapping type (like :class:`dict`). |
| 1418 | |
| 1419 | Raises :class:`TypeError` if `document` is not a mapping type, |
| 1420 | or contains keys that are not instances of |
| 1421 | :class:`str'. Raises :class:`~bson.errors.InvalidDocument` |
| 1422 | if `document` cannot be converted to :class:`BSON`. |
| 1423 | |
| 1424 | :param document: mapping type representing a document |
| 1425 | :param check_keys: check if keys start with '$' or |
| 1426 | contain '.', raising :class:`~bson.errors.InvalidDocument` in |
| 1427 | either case |
| 1428 | :param codec_options: An instance of |
| 1429 | :class:`~bson.codec_options.CodecOptions`. |
| 1430 | |
| 1431 | .. versionchanged:: 3.0 |
| 1432 | Replaced `uuid_subtype` option with `codec_options`. |
| 1433 | """ |
| 1434 | return cls(encode(document, check_keys, codec_options)) |
| 1435 | |
| 1436 | def decode( # type:ignore[override] |
| 1437 | self, codec_options: CodecOptions[Any] = DEFAULT_CODEC_OPTIONS |