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.

6a1345d8e6dd15b2c78eff0c331963b1?s=128

Denis Brumann

November 10, 2020
Tweet

Transcript

  1. Serializer Demystified

  2. SensioLabs Deutschland denis.brumann@sensiolabs.de @dbrumann

  3. None
  4. None
  5. None
  6. None
  7. None
  8. None
  9. None
  10. 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); }
  11. None
  12. 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); }
  13. None
  14. None
  15. None
  16. None
  17. None
  18. None
  19. None
  20. None
  21. None
  22. None
  23. None
  24. 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()
  25. None
  26. None
  27. None
  28. None
  29. None
  30. None
  31. None
  32. None
  33. 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
  34. Symfony\Component\Serializer\Normalizer\ CacheableSupportsMethodInterface interface CacheableSupportsMethodInterface { public function hasCacheableSupportsMethod(): bool; }

  35. None
  36. None
  37. None
  38. None
  39. None
  40. None
  41. None
  42. None
  43. None
  44. None
  45. None
  46. None
  47. 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)
  48. None