Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass mimetype to serializers, and support mimetype parameters in registry #919

Open
danielballan opened this issue Mar 7, 2025 · 0 comments

Comments

@danielballan
Copy link
Member

Tiled "serializers" are functions which take a Python data structure as input and produce bytes as output, for transport over the network. The serializers currently look like this:

def serialize_csv(array, metadata):
if array.ndim > 2:
raise UnsupportedShape(array.shape)
file = io.StringIO()
numpy.savetxt(file, array, fmt="%s", delimiter=",")
return file.getvalue().encode()

These get registered to a mimetype, potentially multiple of them.

serialization_registry.register("array", "text/csv", serialize_csv)
serialization_registry.register("array", "text/x-comma-separated-values", serialize_csv)
serialization_registry.register("array", "text/plain", serialize_csv)

I propose changing the signature to add mimetype as the first argument.

def f(mimetype, array, metadata):
    ...

This is a common pattern in callback registries. (We adopted the same pattern in caproto.) It enables the same callback to be subscribed to multiple channels and easily distinguish which one of them is calling. I see two use cases:

  1. Serializers that can handle many types but need to do different things (like PIL). We currently handle this with a convenience function.
  2. Support for mimetype parameters, such as text/csv;header=present and text/csv;header=absent. It would be convenient for serializer_csv to be called in both cases, and to be able to adjust its output correctly.

Also, in support of (2), I think this the registry should be tweaked to support optional mimetype parameters, such as text/csv;header=present. When the registry is passed a mimetype with parameters, it should perform dispatch based on the type without arguments (text/csv) but it should pass the full mimetype with arguments to the serializer, which can then parse the mimetype and do what it likes with the arguments.

@danielballan danielballan changed the title Pass mimetype to serializres, and support mimetype parameters in registry Pass mimetype to serializers, and support mimetype parameters in registry Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant