Hi, is there a `Serializer` that serializes `str`?
# ask-community
l
Hi, is there a
Serializer
that serializes
str
?
I.e.
str
is the input type for
.serialize
and the output type of
.deserialize
?
specifically, I'm trying to write text as a file on S3...
@task(result=S3Result(bucket=*, location=*, serializer=StringSerializer???()))
c
Hi @Luke Orland - I believe the
JSONSerializer
will do what you’re looking for
l
Thanks, Chris. My use case here is checkpointing the output of a
prefect.tasks.jupyter.ExecuteNotebook
task, which is a JSON string. To store that as an
.ipynb
file, I was converting that json to a dict so that the
JSONSerializer
would dump it back to JSON string. My approach that seems to be working is to subclass
Serializer
, with these implementations:
snippet.py
and then the str output of the ExecuteNotebook can be written out as a
.ipynb
file without the json -> dict -> json round trip.
c
Oh I understand your use case - nice! Your serializer definitely makes sense; I see two ways we might include this back into the library: - include your
StringSerializer
implementation into the serializer library - change the return type of
ExecuteNotebook
to be an actual dictionary instead of a JSON string; this way the
JSONSerializer
works out-of-the-box I would prefer the latter although I’m not sure if there was a reason for the JSON string return type; whichever way we go, it would be interesting to allow for specifying serializers at the task level so that we could default to these serializers for this task so other users don’t have to worry about it.
l
Yeah, that's a nice idea!
c
I’ll open an issue for us: @Marvin open “Change return type of ExecuteNotebook and allow tasks to specify default result serializers”
l
For the ExecuteNotebook task, there are 2
output_format
options--JSON and HTML. The JSON output, if written to a
.ipynb
file, can be read as a notebook file by Jupyter. For HTML output, that
.html
file can be rendered by a web browser. Either way, the output is
str
and should be serialized by something like a
StringSerializer
.
dict
output from that task wouldn't really work if HTML is the selected
output_format
.