Per-request serializer selection

I want to extend plone.restapi serialization to allow selection of per-request serializer selection as follows:

  1. if a GET request for a page passes a parameter richtext=raw is passed in querystring, and collective.exportimport is installed, a subscriber to a publication event (e.g. IPubStart or IPubAfterTraversal), mark request object with collective.exportimport.interfaces.IRawRichTextMarker — expected result should be raw stored text from rich text field, untransformed.
  2. If GET request for a page passes parameter richtext=mycustomthing (or similar), I likewise mark a custom marker interface to trigger use of a custom serializer of my own construction.

My larger goal is having text authored in Plone, exposed over GET request via plone.restapi but suitable for consumption in multiple other systems — with the idea that extending an out-of-the-box API is preferable design to maintaining custom views that do the same thing. I want to double-check that I am not missing some more obvious extension mechanism to pivot serialization per-request in plone.restapi?

I don't think you're missing a more obvious extension mechanism. plone.rest supports routing to different API services based on the request's Accept header, but in this case it sounds like you want to use the same services with different serializers.

In general the result of serializers is not raw data to be returned in a response, but a Python data structure which is in some cases processed further, or dumped to JSON in the base Service class's render method: https://github.com/plone/plone.restapi/blob/main/src/plone/restapi/services/__init__.py#L22 -- so I suspect you would need to patch that method in order to make services return something that is not JSON in certain cases.

1 Like