@@ -57,6 +57,23 @@ def coerce_to_strict(const):
57
57
return const
58
58
59
59
60
+ _swap = (
61
+ ("<" , "\\ u003c" ),
62
+ (">" , "\\ u003e" ),
63
+ ("/" , "\\ u002f" ),
64
+ ("\u2028 " , "\\ u2028" ),
65
+ ("\u2029 " , "\\ u2029" ),
66
+ )
67
+
68
+
69
+ def _safe (json_str ):
70
+ out = json_str
71
+ for unsafe_char , safe_char in _swap :
72
+ if unsafe_char in out :
73
+ out = out .replace (unsafe_char , safe_char )
74
+ return out
75
+
76
+
60
77
def to_json_plotly (plotly_object , pretty = False , engine = None ):
61
78
"""
62
79
Convert a plotly/Dash object to a JSON string representation
@@ -120,7 +137,7 @@ def to_json_plotly(plotly_object, pretty=False, engine=None):
120
137
121
138
from _plotly_utils .utils import PlotlyJSONEncoder
122
139
123
- return json .dumps (plotly_object , cls = PlotlyJSONEncoder , ** opts )
140
+ return _safe ( json .dumps (plotly_object , cls = PlotlyJSONEncoder , ** opts ) )
124
141
elif engine == "orjson" :
125
142
JsonConfig .validate_orjson ()
126
143
opts = orjson .OPT_NON_STR_KEYS | orjson .OPT_SERIALIZE_NUMPY
@@ -136,7 +153,7 @@ def to_json_plotly(plotly_object, pretty=False, engine=None):
136
153
137
154
# Try without cleaning
138
155
try :
139
- return orjson .dumps (plotly_object , option = opts ).decode ("utf8" )
156
+ return _safe ( orjson .dumps (plotly_object , option = opts ).decode ("utf8" ) )
140
157
except TypeError :
141
158
pass
142
159
@@ -146,7 +163,7 @@ def to_json_plotly(plotly_object, pretty=False, engine=None):
146
163
datetime_allowed = True ,
147
164
modules = modules ,
148
165
)
149
- return orjson .dumps (cleaned , option = opts ).decode ("utf8" )
166
+ return _safe ( orjson .dumps (cleaned , option = opts ).decode ("utf8" ) )
150
167
151
168
152
169
def to_json (fig , validate = True , pretty = False , remove_uids = True , engine = None ):
0 commit comments