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

Symfony Live 2017 - Serializer

Symfony Live 2017 - Serializer

Grégoire Pineau

March 31, 2017
Tweet

More Decks by Grégoire Pineau

Other Decks in Technology

Transcript

  1. I am Grégoire Pineau DevOps @blackfireio / @sensiolabs @symfony Core

    Contributor Github / Twitter : @lyrixx Hello! 2
  2. “ Serialization is the process of translating data structures or

    object state into a format that can be stored and reconstructed later in the same or another computer environment. Adapted from en.wikipedia.org/wiki/Serialization 4
  3. When? ◉ To store data in a DB (RDBMS, Redis,

    Elasticsearch) ◉ To send / receive data via an API ◉ To send / receive data via messaging system (AMQP) ◉ To detect changes in time-varying data 5
  4. Why? ◉ PHP serialize / unserialize functions are powerful but:

    ◦ Not interoperable ◦ Not configurable 6
  5. Different formats ◉ Binary: ◦ Faster ◦ Smaller ◦ Ex:

    protobuf, Apache Thrift, … ◉ Textual ◦ Human readable / editable ◦ Ex: CSV, JSON, XML, YAML, ... 7
  6. Before symfony 2.7 ◉ JMSSerializer: ◦ Had more features ◦

    Had a better integration with others libs ◦ Was more used ◉ Symfony Serializer ◦ Was not really used 10
  7. Symfony 2.7 & next ◉ Tons of new features ◉

    … and keeps counting ◉ Better integration with others lib ◉ Simpler than JMSSerializer (?) 12
  8. 14

  9. 19

  10. Normalizers AbstractNormalizer, AbstractObjectNormalizer, ArrayDenormalizer, ContextAwareDenormalizerInterface, ContextAwareNormalizerInterface, CustomNormalizer, DataUriNormalizer, DateTimeNormalizer, DenormalizableInterface,

    DenormalizerAwareInterface, DenormalizerAwareTrait, DenormalizerInterface, GetSetMethodNormalizer, JsonSerializableNormalizer, NormalizableInterface, NormalizerAwareInterface, NormalizerAwareTrait, NormalizerInterface, ObjectNormalizer, PropertyNormalizer, SerializerAwareNormalizer 21
  11. Object Normalizers GetSetMethodNormalizer Uses getters and setters PropertyNormalizer Uses Reflection

    CustomNormalizer Uses $object->normalize(); ObjectNormalizer Uses PropertyAccess Component 22
  12. Extra Normalizer DateTimeNormalizer Turns DateTime into string DataUriNormalizer Turns SplFileInfo

    into data:uri ArrayDenormalizer Denormalizes arrays of objects JsonSerializableNormalizer Turns JsonSerializable into a string 23
  13. 26

  14. 31

  15. Groups ◉ Choose what property is (de) serialized ◉ Could

    be used: ◦ For read / write access ◦ For security (public, connected user, admin user) ◦ For API versioning 39
  16. Tips Use the following format for your Groups: 41 GET

    /posts post_list_read GET /posts/12 post_read PUT /posts/12 post_write GET /posts/12/details post_details_read
  17. NameConverter Tweak the name of a property: ◉ By default,

    it uses the property name (CamelCase) ◉ Automatically convert to snake_case ◉ Rename / Alias some property 46
  18. Custom Object Normalizer ◉ Allow to tweak at PHP level

    the normalization ◉ Be able to add or remove some data: ◦ Canonical URL ◦ “Computed” data: the number of post’s comment 49
  19. Circular Reference ◉ Example: “Post” has a one-to-many relation “Comment”.

    A “Comment” has a property “Post”. ◉ By default the serializer will throw an exception because of the circular reference ◉ But you can implement a Circular Reference Handler 55
  20. Solution 72 You need to create a TagDenormalizer to handle

    the situation: search the tag in DB: ◉ If exists, uses it ◉ If not, create it
  21. 76

  22. Summary 77 ◉ Use Groups ◉ Create a normalizer for

    each serialized entity ◦ Service tag: ‘serializer.normalizer’ ◉ Create a circular reference handler