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. 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); }
  2. 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); }
  3. 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()
  4. 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
  5. 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)