Skip to content

Commit 97b9a33

Browse files
PYTHON-4179: Optimize JSON decoding performance by avoiding object_pairs_hook (#1493)
1 parent 62c6d0f commit 97b9a33

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

bson/json_util.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,11 @@ def loads(s: Union[str, bytes, bytearray], *args: Any, **kwargs: Any) -> Any:
497497
Accepts optional parameter `json_options`. See :class:`JSONOptions`.
498498
"""
499499
json_options = kwargs.pop("json_options", DEFAULT_JSON_OPTIONS)
500-
kwargs["object_pairs_hook"] = lambda pairs: object_pairs_hook(pairs, json_options)
500+
# Execution time optimization if json_options.document_class is dict
501+
if json_options.document_class is dict:
502+
kwargs["object_hook"] = lambda obj: object_hook(obj, json_options)
503+
else:
504+
kwargs["object_pairs_hook"] = lambda pairs: object_pairs_hook(pairs, json_options)
501505
return json.loads(s, *args, **kwargs)
502506

503507

doc/contributors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,4 @@ The following is a list of people who have contributed to
9999
- Iris Ho (sleepyStick)
100100
- Stephan Hof (stephan-hof)
101101
- Casey Clements (caseyclements)
102+
- Ivan Lukyanchikov (ilukyanchikov)

0 commit comments

Comments
 (0)