@@ -32,12 +32,16 @@ def default_engine(self, val):
32
32
)
33
33
34
34
if val == "orjson" :
35
- orjson = get_module ("orjson" )
36
- if orjson is None :
37
- raise ValueError ("The orjson engine requires the orjson package" )
35
+ self .validate_orjson ()
38
36
39
37
self ._default_engine = val
40
38
39
+ @classmethod
40
+ def validate_orjson (cls ):
41
+ orjson = get_module ("orjson" )
42
+ if orjson is None :
43
+ raise ValueError ("The orjson engine requires the orjson package" )
44
+
41
45
42
46
config = JsonConfig ()
43
47
@@ -106,8 +110,6 @@ def to_plotly_json(plotly_object, pretty=False, engine=None):
106
110
"image" : get_module ("PIL.Image" , should_load = False ),
107
111
}
108
112
109
- orjson = get_module ("orjson" , should_load = True )
110
-
111
113
# Dump to a JSON string and return
112
114
# --------------------------------
113
115
if engine in ("json" , "legacy" ):
@@ -148,6 +150,7 @@ def to_plotly_json(plotly_object, pretty=False, engine=None):
148
150
149
151
return json .dumps (plotly_object , cls = PlotlyJSONEncoder , ** opts )
150
152
elif engine == "orjson" :
153
+ JsonConfig .validate_orjson ()
151
154
opts = orjson .OPT_SORT_KEYS | orjson .OPT_SERIALIZE_NUMPY
152
155
153
156
if pretty :
@@ -287,6 +290,8 @@ def from_plotly_json(value, engine=None):
287
290
-------
288
291
dict
289
292
"""
293
+ orjson = get_module ("orjson" , should_load = True )
294
+
290
295
# Validate value
291
296
# --------------
292
297
if not isinstance (value , (string_types , bytes )):
@@ -298,8 +303,6 @@ def from_plotly_json(value, engine=None):
298
303
)
299
304
)
300
305
301
- orjson = get_module ("orjson" , should_load = True )
302
-
303
306
# Determine json engine
304
307
if engine is None :
305
308
engine = config .default_engine
@@ -313,6 +316,7 @@ def from_plotly_json(value, engine=None):
313
316
raise ValueError ("Invalid json engine: %s" % engine )
314
317
315
318
if engine == "orjson" :
319
+ JsonConfig .validate_orjson ()
316
320
# orjson handles bytes input natively
317
321
value_dict = orjson .loads (value )
318
322
else :
0 commit comments