Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions plotly/io/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from _plotly_utils.optional_imports import get_module
from _plotly_utils.basevalidators import ImageUriValidator

_ORJSON_MODULE = get_module("orjson", should_load=True)

_VALIDATE_ORJSON_CALLED = False


# Orca configuration class
# ------------------------
Expand Down Expand Up @@ -325,7 +329,11 @@ def from_json_plotly(value, engine=None):
--------
from_json_plotly : Parse JSON with plotly conventions into a dict
"""
orjson = get_module("orjson", should_load=True)
# Use statically imported _ORJSON_MODULE instead of calling get_module each time
orjson = _ORJSON_MODULE

# Validate value
# --------------

# Validate value
# --------------
Expand All @@ -349,7 +357,12 @@ def from_json_plotly(value, engine=None):
raise ValueError("Invalid json engine: %s" % engine)

if engine == "orjson":
JsonConfig.validate_orjson()
# Cache result of validate_orjson, which is expensive and unnecessary to call each time
global _VALIDATE_ORJSON_CALLED
if not _VALIDATE_ORJSON_CALLED:
config.__class__.validate_orjson() # Preserve side effect and exception raising
_VALIDATE_ORJSON_CALLED = True
# orjson handles bytes input natively
# orjson handles bytes input natively
value_dict = orjson.loads(value)
else:
Expand Down