Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Serializer Demystified (Demo Run)

Serializer Demystified (Demo Run)

The Serializer is an essential tool when building APIs with Symfony. Knowing it in more detail can save you a lot of time when you want to customize how your data is handled. In this talk we will look at how the Symfony Serializer works by default by having a deeper look at the provided Normalizers and then we will look at some use cases for customizing both how data is serialized and deserialized.

This slidedeck is based on a demo project I showed during the Symfony User Group Osnabrück in November as preparation for my talk for the SymfonyWorld.

Denis Brumann

November 10, 2020
Tweet

More Decks by Denis Brumann

Other Decks in Programming

Transcript

  1. Serializer Demystified

    View full-size slide

  2. SensioLabs Deutschland
    [email protected]
    @dbrumann

    View full-size slide

  3. interface EncoderInterface
    {
    public function encode($data, string $format, array $context = []);
    public function supportsEncoding(string $format);
    }
    interface NormalizerInterface
    {
    public function normalize($object, string $format = null, array $context = []);
    public function supportsNormalization($data, string $format = null);
    }

    View full-size slide

  4. interface DenormalizerInterface
    {
    public function denormalize($data, string $type, string $format = null, array
    $context = []);
    public function supportsDenormalization($data, string $type, string $format =
    null);
    }
    interface DecoderInterface
    {
    public function decode(string $data, string $format, array $context = []);
    public function supportsDecoding(string $format);
    }

    View full-size slide

  5. Key Default Value Comment
    JsonEncode::OPTIONS 0 json_encode()-options bitmask
    JsonDecode::OPTIONS 0 json_decode()-options bitmask
    JsonDecode::ASSOCIATIVE false Whether json_decode() should return
    associative array or stdClass
    JsonDecode::RECURSION_DEPTH 512 Recursion depth for json_decode()

    View full-size slide

  6. GetSetMethodNormalizer get…, is…, has…
    set…
    PropertyNormalizer Reflection for accessing class attributes
    ObjectNormalizer
    Uses Symfony PropertyAccess
    get…, is…, has…
    public properties
    add…/remove… for single entry in collections

    View full-size slide

  7. Symfony\Component\Serializer\Normalizer\
    CacheableSupportsMethodInterface
    interface CacheableSupportsMethodInterface
    {
    public function hasCacheableSupportsMethod(): bool;
    }

    View full-size slide

  8. AbstractNormalizer:: Keys Description
    ALLOW_EXTRA_ATTRIBUTES On denormalization extra attributes, that do not match
    the object, are ignored instead of throwing an error
    ATTRIBUTES
    Array of class properties that should be
    encoded/decoded, including nested properties, e.g.:
    ['familyName', 'company' => ['name']]
    IGNORED_ATTRIBUTES List of attributes to be ignored on encoding/decoding
    GROUPS Serialization groups to consider for encoding/decoding
    (can be specified with annotation on class properties)

    View full-size slide