Symfony Live 2017 - Serializer

Symfony Live 2017 - Serializer

7602f2751868682b296171f58589c851?s=128

Grégoire Pineau

March 31, 2017
Tweet

Transcript

  1. SymfonyLive Paris 2017 The Serializer Component

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

    Contributor Github / Twitter : @lyrixx Hello! 2
  3. 3 Serialization ?

  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
  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
  6. Why? ◉ PHP serialize / unserialize functions are powerful but:

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

    protobuf, Apache Thrift, … ◉ Textual ◦ Human readable / editable ◦ Ex: CSV, JSON, XML, YAML, ... 7
  8. symfony/serializer 8

  9. History

  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
  11. History 11

  12. Symfony 2.7 & next ◉ Tons of new features ◉

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

  14. 14

  15. 15 And Decoders Encoders

  16. Encoders ◉ CsvEncoder ◉ JsonEncoder ◉ XmlEncoder ◉ YamlEncoder 16

  17. Encoder usage 17

  18. Decoder usage 18

  19. 19

  20. 20 And denormalizers Normalizers

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

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

    CustomNormalizer Uses $object->normalize(); ObjectNormalizer Uses PropertyAccess Component 22
  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
  24. Normalizer usage 24

  25. Denormalizer usage 25

  26. 26

  27. 27 And deserializer Serializer

  28. Serializer 28

  29. Serializer Usage 29

  30. Deserializer Usage 30

  31. 31

  32. symfony/symfony-demo 32

  33. 33 https://github.com/symfony/symfony-demo

  34. Enable the serializer 34

  35. Let’s build an API on top of symfony/demo

  36. Create a Controller 36

  37. Try it 37

  38. Configuration Groups 38

  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
  40. Groups 40

  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
  42. Groups 42 Context

  43. Configuration MaxDepth 43

  44. MaxDepth 44

  45. Configuration NameConverter 45

  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
  47. Try it again 47

  48. Add more data ... … to the representation 48

  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
  50. PostNormalizer 50

  51. PostNormalizer 51

  52. Custom Object Normalizer 52

  53. Try it again 53

  54. 54 Circular Reference

  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
  56. Circular Reference 56

  57. Circular Reference 57

  58. Circular Reference SF 3.3 58

  59. Circular Reference < SF 3.2 59

  60. Deserialization Aka: How to use user data 60

  61. Create a Controller 61

  62. Create a Controller 62

  63. ConstraintViolation ListNormalizer 63

  64. Send invalid data 64

  65. Send valid data 65

  66. Deserialization Into an Object 66

  67. Into an Object 67

  68. Deserialization With some relations 68

  69. Post Object 69

  70. Enable the property info component 70

  71. Try it! 71

  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
  73. Deserialization Of array 73

  74. The controller 74

  75. Summary

  76. 76

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

    each serialized entity ◦ Service tag: ‘serializer.normalizer’ ◉ Create a circular reference handler
  78. Any questions ? @lyrixx Thanks! 78