Symfony Live 2017 - Serializer

Symfony Live 2017 - Serializer

7602f2751868682b296171f58589c851?s=128

Grégoire Pineau

March 31, 2017
Tweet

Transcript

  1. 2.

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

    Contributor Github / Twitter : @lyrixx Hello! 2
  2. 4.

    “ 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. 5.

    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. 6.

    Why? ◉ PHP serialize / unserialize functions are powerful but:

    ◦ Not interoperable ◦ Not configurable 6
  5. 7.

    Different formats ◉ Binary: ◦ Faster ◦ Smaller ◦ Ex:

    protobuf, Apache Thrift, … ◉ Textual ◦ Human readable / editable ◦ Ex: CSV, JSON, XML, YAML, ... 7
  6. 9.
  7. 10.

    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
  8. 12.

    Symfony 2.7 & next ◉ Tons of new features ◉

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

    14

  10. 19.

    19

  11. 21.

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

    DenormalizerAwareInterface, DenormalizerAwareTrait, DenormalizerInterface, GetSetMethodNormalizer, JsonSerializableNormalizer, NormalizableInterface, NormalizerAwareInterface, NormalizerAwareTrait, NormalizerInterface, ObjectNormalizer, PropertyNormalizer, SerializerAwareNormalizer 21
  12. 22.

    Object Normalizers GetSetMethodNormalizer Uses getters and setters PropertyNormalizer Uses Reflection

    CustomNormalizer Uses $object->normalize(); ObjectNormalizer Uses PropertyAccess Component 22
  13. 23.

    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
  14. 26.

    26

  15. 31.

    31

  16. 37.
  17. 39.

    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
  18. 40.
  19. 41.

    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
  20. 46.

    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
  21. 49.

    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
  22. 55.

    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
  23. 72.

    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
  24. 75.
  25. 76.

    76

  26. 77.

    Summary 77 ◉ Use Groups ◉ Create a normalizer for

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