Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Serializer.php

class Serializer

Same name in this branch
  1. 11.1.x vendor/symfony/serializer/Serializer.php \Symfony\Component\Serializer\Serializer
  2. 11.1.x vendor/phpdocumentor/reflection-docblock/src/DocBlock/Serializer.php \phpDocumentor\Reflection\DocBlock\Serializer
  3. 11.1.x core/modules/rest/src/Plugin/views/style/Serializer.php \Drupal\rest\Plugin\views\style\Serializer

Overrides the Symfony serializer to cordon off our incompatible normalizers.

This service is for *internal* use only. It is not suitable for *any* reuse. Backwards compatibility is in no way guaranteed and will almost certainly be broken in the future.

@link https://www.drupal.org/project/drupal/issues/2923779#comment-12407443

@internal JSON:API maintains no PHP API since its API is the HTTP API. This class may change at any time and this will break any dependencies on it.

Hierarchy

  • class \Symfony\Component\Serializer\Serializer implements \Symfony\Component\Serializer\SerializerInterface, \Symfony\Component\Serializer\Normalizer\NormalizerInterface, \Symfony\Component\Serializer\Normalizer\DenormalizerInterface, \Symfony\Component\Serializer\Encoder\ContextAwareEncoderInterface, \Symfony\Component\Serializer\Encoder\ContextAwareDecoderInterface
    • class \Drupal\jsonapi\Serializer\Serializer extends \Symfony\Component\Serializer\Serializer

Expanded class hierarchy of Serializer

See also

https://www.drupal.org/project/drupal/issues/3032787

jsonapi.api.php

10 string references to 'Serializer'
jsonapi.services.yml in core/modules/jsonapi/jsonapi.services.yml
core/modules/jsonapi/jsonapi.services.yml
RegisterSerializationClassesCompilerPass::process in core/modules/serialization/src/RegisterSerializationClassesCompilerPass.php
You can modify the container here before it is dumped to PHP code.
RequestHandler::create in core/modules/rest/src/RequestHandler.php
Instantiates a new instance of this class.
RestExport::defineOptions in core/modules/rest/src/Plugin/views/display/RestExport.php
Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase:defineOptions().
Serializer::create in core/modules/rest/src/Plugin/views/style/Serializer.php
Creates an instance of the plugin.

... See full list

1 service uses Serializer
jsonapi.serializer in core/modules/jsonapi/jsonapi.services.yml
Drupal\jsonapi\Serializer\Serializer

File

core/modules/jsonapi/src/Serializer/Serializer.php, line 23

Namespace

Drupal\jsonapi\Serializer
View source
final class Serializer extends SymfonySerializer {
    
    /**
     * A normalizer to fall back on when JSON:API cannot normalize an object.
     *
     * @var \Symfony\Component\Serializer\Normalizer\NormalizerInterface|\Symfony\Component\Serializer\Normalizer\DenormalizerInterface
     */
    protected $fallbackNormalizer;
    
    /**
     * {@inheritdoc}
     */
    public function __construct(array $normalizers = [], array $encoders = []) {
        foreach ($normalizers as $normalizer) {
            if (!str_starts_with(get_class($normalizer), 'Drupal\\jsonapi\\Normalizer')) {
                throw new \LogicException('JSON:API does not allow adding more normalizers!');
            }
        }
        parent::__construct($normalizers, $encoders);
    }
    
    /**
     * Adds a secondary normalizer.
     *
     * This normalizer will be attempted when JSON:API has no applicable
     * normalizer.
     *
     * @param \Symfony\Component\Serializer\Normalizer\NormalizerInterface $normalizer
     *   The secondary normalizer.
     */
    public function setFallbackNormalizer(NormalizerInterface $normalizer) {
        $this->fallbackNormalizer = $normalizer;
    }
    
    /**
     * {@inheritdoc}
     */
    public function normalize($data, $format = NULL, array $context = []) : array|string|int|float|bool|\ArrayObject|null {
        if ($this->selfSupportsNormalization($data, $format, $context)) {
            return parent::normalize($data, $format, $context);
        }
        if ($this->fallbackNormalizer
            ->supportsNormalization($data, $format, $context)) {
            return $this->fallbackNormalizer
                ->normalize($data, $format, $context);
        }
        return parent::normalize($data, $format, $context);
    }
    
    /**
     * {@inheritdoc}
     */
    public function denormalize($data, $type, $format = NULL, array $context = []) : mixed {
        if ($this->selfSupportsDenormalization($data, $type, $format, $context)) {
            return parent::denormalize($data, $type, $format, $context);
        }
        return $this->fallbackNormalizer
            ->denormalize($data, $type, $format, $context);
    }
    
    /**
     * {@inheritdoc}
     */
    public function supportsNormalization($data, ?string $format = NULL, array $context = []) : bool {
        return $this->selfSupportsNormalization($data, $format, $context) || $this->fallbackNormalizer
            ->supportsNormalization($data, $format, $context);
    }
    
    /**
     * Checks whether this class alone supports normalization.
     *
     * @param mixed $data
     *   Data to normalize.
     * @param string $format
     *   The format being (de-)serialized from or into.
     * @param array $context
     *   (optional) Options available to the normalizer.
     *
     * @return bool
     *   Whether this class supports normalization for the given data.
     */
    private function selfSupportsNormalization($data, $format = NULL, array $context = []) {
        return parent::supportsNormalization($data, $format, $context);
    }
    
    /**
     * {@inheritdoc}
     */
    public function supportsDenormalization($data, string $type, ?string $format = NULL, array $context = []) : bool {
        return $this->selfSupportsDenormalization($data, $type, $format, $context) || $this->fallbackNormalizer
            ->supportsDenormalization($data, $type, $format, $context);
    }
    
    /**
     * Checks whether this class alone supports denormalization.
     *
     * @param mixed $data
     *   Data to denormalize from.
     * @param string $type
     *   The class to which the data should be denormalized.
     * @param string $format
     *   The format being deserialized from.
     * @param array $context
     *   (optional) Options available to the denormalizer.
     *
     * @return bool
     *   Whether this class supports normalization for the given data and type.
     */
    private function selfSupportsDenormalization($data, $type, $format = NULL, array $context = []) {
        return parent::supportsDenormalization($data, $type, $format, $context);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS public constant
Serializer::$decoder protected property
Serializer::$denormalizerCache private property
Serializer::$encoder protected property
Serializer::$fallbackNormalizer protected property A normalizer to fall back on when JSON:API cannot normalize an object.
Serializer::$normalizerCache private property
Serializer::decode final public function Decodes a string into PHP data. Overrides DecoderInterface::decode
Serializer::denormalize public function Denormalizes data back into an object of the given class. Overrides Serializer::denormalize
Serializer::deserialize final public function Deserializes data into the given type. Overrides SerializerInterface::deserialize
Serializer::EMPTY_ARRAY_AS_OBJECT public constant Flag to control whether an empty array should be transformed to an
object (in JSON: {}) or to a list (in JSON: []).
Serializer::encode final public function Encodes data into the given format. Overrides EncoderInterface::encode
Serializer::getDenormalizer private function Returns a matching denormalizer.
Serializer::getNormalizer private function Returns a matching normalizer.
Serializer::getSupportedTypes public function Returns the types potentially supported by this normalizer. Overrides NormalizerInterface::getSupportedTypes
Serializer::normalize public function Normalizes data into a set of arrays/scalars. Overrides Serializer::normalize
Serializer::SCALAR_TYPES private constant
Serializer::selfSupportsDenormalization private function Checks whether this class alone supports denormalization.
Serializer::selfSupportsNormalization private function Checks whether this class alone supports normalization.
Serializer::serialize final public function Serializes data in the appropriate format. Overrides SerializerInterface::serialize
Serializer::setFallbackNormalizer public function Adds a secondary normalizer.
Serializer::supportsDecoding public function Checks whether the deserializer can decode from given format. Overrides ContextAwareDecoderInterface::supportsDecoding
Serializer::supportsDenormalization public function Checks whether the given class is supported for denormalization by this normalizer. Overrides Serializer::supportsDenormalization
Serializer::supportsEncoding public function Checks whether the serializer can encode to given format. Overrides ContextAwareEncoderInterface::supportsEncoding
Serializer::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. Overrides Serializer::supportsNormalization
Serializer::__construct public function Overrides Serializer::__construct

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal