Normalizers

Normalizers

Normalizers turn objects into arrays and vice versa. They implement Symfony\Component\Serializer\Normalizer\NormalizerInterface for normalizing (object to array) and Symfony\Component\Serializer\Normalizer\DenormalizerInterface for denormalizing (array to object).

Normalizers are enabled in the serializer passing them as its first argument:

  1. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  2. use Symfony\Component\Serializer\Serializer;
  3. $normalizers = [new ObjectNormalizer()];
  4. $serializer = new Serializer($normalizers);

Built-in Normalizers

Symfony includes the following normalizers but you can also create your own normalizer:

  • Symfony\Component\Serializer\Normalizer\ObjectNormalizer to normalize PHP object using the PropertyAccess component;
  • Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer for [DateTimeZone](https://www.php.net/manual/en/class.datetimezone.php "DateTimeZone") objects
  • Symfony\Component\Serializer\Normalizer\DateTimeNormalizer for objects implementing the [DateTimeInterface](https://www.php.net/manual/en/class.datetimeinterface.php "DateTimeInterface") interface
  • Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer for [DateInterval](https://www.php.net/manual/en/class.dateinterval.php "DateInterval") objects
  • Symfony\Component\Serializer\Normalizer\DataUriNormalizer to transform [SplFileInfo](https://www.php.net/manual/en/class.splfileinfo.php "SplFileInfo") objects in Data URIs
  • Symfony\Component\Serializer\Normalizer\CustomNormalizer to normalize PHP object using an object that implements Symfony\Component\Serializer\Normalizer\NormalizableInterface;
  • Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer to normalize PHP object using the getter and setter methods of the object;
  • Symfony\Component\Serializer\Normalizer\PropertyNormalizer to normalize PHP object using PHP reflection.
  • Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer for objects implementing the Symfony\Component\Validator\ConstraintViolationListInterface interface
  • Symfony\Component\Serializer\Normalizer\ProblemNormalizer for Symfony\Component\ErrorHandler\Exception\FlattenException objects
  • Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer to deal with objects implementing the [JsonSerializable](https://www.php.net/manual/en/class.jsonserializable.php "JsonSerializable") interface

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.