Skip to content

Commit 2a58868

Browse files
authored
Merge pull request #374 from simvue-io/373-offline-mode-doesnt-work-when-saving-objects
Use BytesIO for all serializations
2 parents 06fed7b + 19f539a commit 2a58868

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

simvue/serialization.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ def _serialize_plotly_figure(data: typing.Any) -> typing.Optional[tuple[str, str
9999
return None
100100
mimetype = "application/vnd.plotly.v1+json"
101101
data = plotly.io.to_json(data, engine="json")
102+
mfile = BytesIO()
103+
mfile.write(data.encode())
104+
mfile.seek(0)
105+
data = mfile.read()
102106
return data, mimetype
103107

104108

@@ -110,6 +114,10 @@ def _serialize_matplotlib(data: typing.Any) -> typing.Optional[tuple[str, str]]:
110114
return None
111115
mimetype = "application/vnd.plotly.v1+json"
112116
data = plotly.io.to_json(plotly.tools.mpl_to_plotly(data.gcf()), engine="json")
117+
mfile = BytesIO()
118+
mfile.write(data.encode())
119+
mfile.seek(0)
120+
data = mfile.read()
113121
return data, mimetype
114122

115123

@@ -121,6 +129,10 @@ def _serialize_matplotlib_figure(data: typing.Any) -> typing.Optional[tuple[str,
121129
return None
122130
mimetype = "application/vnd.plotly.v1+json"
123131
data = plotly.io.to_json(plotly.tools.mpl_to_plotly(data), engine="json")
132+
mfile = BytesIO()
133+
mfile.write(data.encode())
134+
mfile.seek(0)
135+
data = mfile.read()
124136
return data, mimetype
125137

126138

@@ -161,8 +173,11 @@ def _serialize_torch_tensor(data: typing.Any) -> typing.Optional[tuple[str, str]
161173
def _serialize_json(data: typing.Any) -> typing.Optional[tuple[str, str]]:
162174
mimetype = "application/json"
163175
try:
164-
data = json.dumps(data)
165-
except TypeError:
176+
mfile = BytesIO()
177+
mfile.write(json.dumps(data).encode())
178+
mfile.seek(0)
179+
data = mfile.read()
180+
except (TypeError, json.JSONDecodeError):
166181
return None
167182
return data, mimetype
168183

0 commit comments

Comments
 (0)