As part of the flexible BSON epic, we should implement support for registering callbacks to handle simple types that the bson module doesn't support natively.
PYTHON-1731 provides a good example. PyMongo doesn't support decimal.Decimal, but does have a related type, decimal128.Decimal128. An application should be able to register a handler to automatically attempt conversion of Decimal to Decimal128. The callbacks could be defined something like this:
This would be a granular replacement for SON manipulators for simple types, where conversion is handled at the BSON layer. By comparison SON manipulators (which have been deprecated since PyMongo 3.0) require an application to iterate an entire document looking for types to convert, causing two full iterations of each document on encoding or decoding.
This work should be extended to the json_util module as well.
For complex types (for example, custom document models) PYTHON-1679 will provide far more flexibility to directly control BSON encoding (BSON writer) and decoding (BSON reader) than most applications require.