-
Type: Improvement
-
Resolution: Won't Fix
-
Priority: Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: BSON
-
None
When serving data through Rest APIs the ObjectId has to be converted (to a dict when serving JSON) every time, which adds the complexity of needing to change/parse it and also could potentially slow the API when there is a lot of data to send. And although adding a function to convert the ObjectId to a dictionary doesn't follow the guidelines, It would be a great quality of life improvement. I've made already a working solution for this and works fine when sending the data using FastApi.
My solution was to:
- Add _dict_ attribute to _slots_
__slots__ = ("__id", "__dict__")
- Set _dict attribute to be self._id
# This is were self.__id is set. if oid is None: self.__generate() elif isinstance(oid, bytes) and len(oid) == 12: self.__id = oid self.__dict__ = {"$oid": binascii.hexlify(self.__id).decode()} # my changes else: self.__validate(oid)
- is related to
-
PYTHON-3397 Update docs for ObjectId and other BSON types to link to Extended JSON examples
- Backlog
-
CDRIVER-4453 libbson is slow to JSON encode and decode large strings
- In Progress
- related to
-
PYTHON-1374 Optimize json_util performance
- Closed