Define a normalizer just for a specific use case I try to create a custom API with custom endpoints where API users can receive entity data from our page. We want to have flat arrays for most of the fields and some additional custom data added so I tried to implement some custom normalizer classes. I found out that supportsNormalization() is the place where the decision is made if my normalizer is used, but I have no context. I just have the object which needs normalization and the format on which I can say Yes, please use my normalizer. As I don't want to force all my entities everywhere in the project be normalized with flat array this is too restrictive for us. I want to use my normalizer only when the entities should be normalized for delivering it in a specific API. Is there a way to achieve this?
The supportsNormalization method comes from the Symfony Serializer component, and for this reason it is up to the implementation to decide what to do with these items.

However Drupal core serialization module provides a nice base class, ComplexDataNormalizer, which is probably the class your Normalizer should extend when you need to normalize a complex data type such as an Entity, Map or a list of those things.

Classes that extend the base class should provide a unique interface to target: ComplexDataNormalizer::$supportedInterfaceOrClass.

With the Symfony service locator, as long as the normalizer weight is adjusted for any conflicts, then it will be used. A good example of this is to look at the core hal module.

So for instance, in my module xero, I provide my own Typed Data data types to interact with a third party API and the top-level data types I have my own interface.

Example:

class XeroNormalizer extends ComplexDataNormalizer implements DenormalizerInterface {

  protected $supportedInterfaceOrClass = 'Drupal\xero\TypedData\XeroTypeInterface';


You can read more about how the Drupal serialization module works in the documentation: https://www.drupal.org/docs/8/api/serialization-api/how-the-serializer-works