-
Type: Improvement
-
Resolution: Fixed
-
Priority: Unknown
-
Affects Version/s: None
-
Component/s: JSON, Performance
-
None
Context
When encoding ints, floats, str, or None (depending on the JSONOptions set) our encoder will raise a TypeError and then catch the error and suppress it:
elif json_options.json_mode == JSONMode.CANONICAL and isinstance(obj, int): if -(2**31) <= obj < 2**31: return {"$numberInt": str(obj)} return {"$numberLong": str(obj)} elif json_options.json_mode != JSONMode.LEGACY and isinstance(obj, float): if math.isnan(obj): return {"$numberDouble": "NaN"} elif math.isinf(obj): representation = "Infinity" if obj > 0 else "-Infinity" return {"$numberDouble": representation} elif json_options.json_mode == JSONMode.CANONICAL: # repr() will return the shortest string guaranteed to produce the # original value, when float() is called on it. return {"$numberDouble": str(repr(obj))} raise TypeError("%r is not JSON serializable" % obj) # <---- Will be used for ints/floats
This results in very poor performance. Instead we can skip the TypeError and directly return the int/float:
- is related to
-
PYTHON-4144 Optimize json_util encoding performance using single dispatch table
- Closed
- related to
-
PYTHON-1374 Optimize json_util performance
- Closed