Normalizers
Warning: You are browsing the documentation for Symfony 3.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.2 (the current stable version).
Normalizers turn objects into arrays and vice versa. They implement NormalizerInterface for normalizing (object to array) and DenormalizerInterface for denormalizing (array to object).
Normalizers are enabled in the serializer passing them as its first argument:
1 2 3 4 5
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
$normalizers = [new ObjectNormalizer()];
$serializer = new Serializer($normalizers);
Built-in Normalizers
Symfony includes the following normalizers but you can also create your own normalizer:
- ObjectNormalizer to normalize PHP object using the PropertyAccessor component;
- DateTimeNormalizer for objects implementing the DateTimeInterface interface
- DateIntervalNormalizer for DateInterval objects
- DataUriNormalizer to transform SplFileInfo objects in Data URIs
- CustomNormalizer to normalize PHP object using an object that implements NormalizableInterface;
- GetSetMethodNormalizer to normalize PHP object using the getter and setter methods of the object;
- PropertyNormalizer to normalize PHP object using PHP reflection.
- JsonSerializableNormalizer to deal with objects implementing the JsonSerializable interface
This work, including the code samples, is licensed under a
Creative Commons BY-SA 3.0 license.