-
Type: New Feature
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: BSON
-
None
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:
# On encoding, the bson module calls the callback registered for a type # it doesn't natively support. That callback must return a type the bson # module does natively support, Decimal128 in this case. def decimal_to_decimal128(obj): return Decimal128(obj) # On decoding, the bson module decodes BSON data to a type it supports, # then calls the callback registered for that type. The callback can return any # type the application desires. In this example it returns a decimal.Decimal, def decimal128_to_decimal(obj): return obj.to_decimal()
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.
- causes
-
PYTHON-1775 Doctests fail on unexpected TypeRegistry in repr
- Closed
- is depended on by
-
PYTHON-381 BSON should support some type of object state callback
- Closed
-
PYTHON-1307 Remove support for SON manipulators
- Closed
-
PYTHON-1731 Need hook for encoding unencodable objects
- Closed
- is documented by
-
PYTHON-1768 Document how to encode subtypes of custom types via callback mechanism
- Closed
- is related to
-
PYTHON-1782 MongoClient fails ConfigurationError: Unknown option type_registry
- Closed
-
PYTHON-1748 Treat Python 2 str as Binary
- Closed
- related to
-
PYTHON-1769 Re-define TypeCodecBase as an AbstractBaseClass
- Closed