-
Type: Task
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 3.7.2
-
Component/s: Codecs
-
None
We have a use-case where we are storing large, nested documents into MongoDB. These documents contain mostly MongoDB-compatible objects, but have `decimal.Decimal` instances in the document. When attempting to store those in the database, it fails with a `bson.errors.InvalidDocument: Cannot encode object`.
Obviously, the thing to do here is to use the bson.decimal128 module to encode these Decimal objects into objects compatible with BSON.
The instructions in the deprecated SONManipulator and the decimal128 module suggest that it should be the responsibility of the application to encode these objects. However, because these objects are large and nested, it's expensive to traverse the whole tree just to replace a few objects before passing to the driver, especially because the driver is going to traverse the document to convert it to BSON.
On the decode side, the CodecOptions provides for a `document_class` that can serve as a hook to decode the values of that document.
What's lacking is a hook to encode values during serialization.
Tools like `json` encoders provide a hook that can be called for each object during the encoding to handle transforming unencodable objects (default argument to dump If the PyMongo driver (or drivers in general) could solicit a similar hook, that would enable a much more efficient handling of these documents during serialization.
- depends on
-
PYTHON-1750 Support codec callbacks for simple types
- Closed