Skip to content

Commit 80be8bd

Browse files
committed
Remove the new "json" encoder
since it's sometimes slower, and never faster, than current encoder. Rename "legacy" to "json".
1 parent 81f73d5 commit 80be8bd

File tree

3 files changed

+13
-44
lines changed

3 files changed

+13
-44
lines changed

Diff for: packages/python/plotly/plotly/basedatatypes.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -3416,9 +3416,8 @@ def to_json(self, *args, **kwargs):
34163416
34173417
engine: str (default None)
34183418
The JSON encoding engine to use. One of:
3419-
- "json" for a rewritten encoder based on the built-in Python json module
3419+
- "json" for an encoder based on the built-in Python json module
34203420
- "orjson" for a fast encoder the requires the orjson package
3421-
- "legacy" for the legacy JSON encoder.
34223421
If not specified, the default encoder is set to the current value of
34233422
plotly.io.json.config.default_encoder.
34243423
@@ -3480,9 +3479,8 @@ def write_json(self, *args, **kwargs):
34803479
34813480
engine: str (default None)
34823481
The JSON encoding engine to use. One of:
3483-
- "json" for a rewritten encoder based on the built-in Python json module
3482+
- "json" for an encoder based on the built-in Python json module
34843483
- "orjson" for a fast encoder the requires the orjson package
3485-
- "legacy" for the legacy JSON encoder.
34863484
If not specified, the default encoder is set to the current value of
34873485
plotly.io.json.config.default_encoder.
34883486

Diff for: packages/python/plotly/plotly/io/_json.py

+9-38
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# Orca configuration class
1414
# ------------------------
1515
class JsonConfig(object):
16-
_valid_engines = ("legacy", "json", "orjson", "auto")
16+
_valid_engines = ("json", "orjson", "auto")
1717

1818
def __init__(self):
1919
self._default_engine = "auto"
@@ -74,7 +74,6 @@ def to_json_plotly(plotly_object, pretty=False, engine=None):
7474
The JSON encoding engine to use. One of:
7575
- "json" for an engine based on the built-in Python json module
7676
- "orjson" for a faster engine that requires the orjson package
77-
- "legacy" for the legacy JSON engine.
7877
- "auto" for the "orjson" engine if available, otherwise "json"
7978
If not specified, the default engine is set to the current value of
8079
plotly.io.json.config.default_engine.
@@ -99,7 +98,7 @@ def to_json_plotly(plotly_object, pretty=False, engine=None):
9998
engine = "orjson"
10099
else:
101100
engine = "json"
102-
elif engine not in ["orjson", "json", "legacy"]:
101+
elif engine not in ["orjson", "json"]:
103102
raise ValueError("Invalid json engine: %s" % engine)
104103

105104
modules = {
@@ -111,43 +110,17 @@ def to_json_plotly(plotly_object, pretty=False, engine=None):
111110

112111
# Dump to a JSON string and return
113112
# --------------------------------
114-
if engine in ("json", "legacy"):
113+
if engine == "json":
115114
opts = {"sort_keys": True}
116115
if pretty:
117116
opts["indent"] = 2
118117
else:
119118
# Remove all whitespace
120119
opts["separators"] = (",", ":")
121120

122-
if engine == "json":
123-
cleaned = clean_to_json_compatible(
124-
plotly_object,
125-
numpy_allowed=False,
126-
datetime_allowed=False,
127-
modules=modules,
128-
)
129-
encoded_o = json.dumps(cleaned, **opts)
130-
131-
if not ("NaN" in encoded_o or "Infinity" in encoded_o):
132-
return encoded_o
133-
134-
# now:
135-
# 1. `loads` to switch Infinity, -Infinity, NaN to None
136-
# 2. `dumps` again so you get 'null' instead of extended JSON
137-
try:
138-
new_o = json.loads(encoded_o, parse_constant=coerce_to_strict)
139-
except ValueError:
140-
# invalid separators will fail here. raise a helpful exception
141-
raise ValueError(
142-
"Encoding into strict JSON failed. Did you set the separators "
143-
"valid JSON separators?"
144-
)
145-
else:
146-
return json.dumps(new_o, **opts)
147-
else:
148-
from _plotly_utils.utils import PlotlyJSONEncoder
121+
from _plotly_utils.utils import PlotlyJSONEncoder
149122

150-
return json.dumps(plotly_object, cls=PlotlyJSONEncoder, **opts)
123+
return json.dumps(plotly_object, cls=PlotlyJSONEncoder, **opts)
151124
elif engine == "orjson":
152125
JsonConfig.validate_orjson()
153126
opts = orjson.OPT_SORT_KEYS | orjson.OPT_SERIALIZE_NUMPY
@@ -197,7 +170,6 @@ def to_json(fig, validate=True, pretty=False, remove_uids=True, engine=None):
197170
The JSON encoding engine to use. One of:
198171
- "json" for an engine based on the built-in Python json module
199172
- "orjson" for a faster engine that requires the orjson package
200-
- "legacy" for the legacy JSON engine.
201173
- "auto" for the "orjson" engine if available, otherwise "json"
202174
If not specified, the default engine is set to the current value of
203175
plotly.io.json.config.default_engine.
@@ -249,7 +221,6 @@ def write_json(fig, file, validate=True, pretty=False, remove_uids=True, engine=
249221
The JSON encoding engine to use. One of:
250222
- "json" for an engine based on the built-in Python json module
251223
- "orjson" for a faster engine that requires the orjson package
252-
- "legacy" for the legacy JSON engine.
253224
- "auto" for the "orjson" engine if available, otherwise "json"
254225
If not specified, the default engine is set to the current value of
255226
plotly.io.json.config.default_engine.
@@ -289,7 +260,7 @@ def from_json_plotly(value, engine=None):
289260
290261
engine: str (default None)
291262
The JSON decoding engine to use. One of:
292-
- if "json" or "legacy", parse JSON using built in json module
263+
- if "json", parse JSON using built in json module
293264
- if "orjson", parse using the faster orjson module, requires the orjson
294265
package
295266
- if "auto" use orjson module if available, otherwise use the json module
@@ -327,7 +298,7 @@ def from_json_plotly(value, engine=None):
327298
engine = "orjson"
328299
else:
329300
engine = "json"
330-
elif engine not in ["orjson", "json", "legacy"]:
301+
elif engine not in ["orjson", "json"]:
331302
raise ValueError("Invalid json engine: %s" % engine)
332303

333304
if engine == "orjson":
@@ -362,7 +333,7 @@ def from_json(value, output_type="Figure", skip_invalid=False, engine=None):
362333
363334
engine: str (default None)
364335
The JSON decoding engine to use. One of:
365-
- if "json" or "legacy", parse JSON using built in json module
336+
- if "json", parse JSON using built in json module
366337
- if "orjson", parse using the faster orjson module, requires the orjson
367338
package
368339
- if "auto" use orjson module if available, otherwise use the json module
@@ -416,7 +387,7 @@ def read_json(file, output_type="Figure", skip_invalid=False, engine=None):
416387
417388
engine: str (default None)
418389
The JSON decoding engine to use. One of:
419-
- if "json" or "legacy", parse JSON using built in json module
390+
- if "json", parse JSON using built in json module
420391
- if "orjson", parse using the faster orjson module, requires the orjson
421392
package
422393
- if "auto" use orjson module if available, otherwise use the json module

Diff for: packages/python/plotly/plotly/tests/test_io/test_to_from_plotly_json.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ def check_roundtrip(value, engine, pretty):
6565

6666
# Fixtures
6767
if orjson is not None:
68-
engines = ["json", "orjson", "legacy", "auto"]
68+
engines = ["json", "orjson", "auto"]
6969
else:
70-
engines = ["json", "legacy", "auto"]
70+
engines = ["json", "auto"]
7171

7272

7373
@pytest.fixture(scope="module", params=engines)

0 commit comments

Comments
 (0)