Discover the Serializer component

34ade09dd3d11004ca8ee4174fd3d6a2?s=47 Sarah KHALIL
September 22, 2017

Discover the Serializer component

The Symfony’s Serializer component exists since version 2 of Symfony, but lately it has been improved and includes a lot of new features.

During this talk, I’ll present the unknown but very powerful features of this library.

After a reminder of the basics, we’ll discover how the component enables the use of any type of PHP object, whatever their styles : getters / setters, public properties, proxys…

Then, we’ll see the different formats natively supported : JSON, XML, YAML and CSV. We’ll also use dates, and mention the file upload with the « data : URI » support.

Finally, we’ll study more complex cases such as choosing the properties to serialize / unserialize thanks to groups, managing circular references, serializing trees by limiting their depth and updating the existing objects.

34ade09dd3d11004ca8ee4174fd3d6a2?s=128

Sarah KHALIL

September 22, 2017
Tweet

Transcript

  1. DISCOVER THE SERIALIZER COMPONENT Sarah Khalil @saro0h

  2. WHAT’S THE PLAN? 1. Serialization? What’s that? 2. Architecture of

    the Serializer component 3. Usage of the Serializer component in Symfony full stack
  3. A BIT OF HISTORY FIRST

  4. “Before 2.7:

  5. None
  6. “From Symfony 2.7 & next Tons of new features …

    and keeps counting Better integration with other libraries As powerful as the JMSSerializer!
  7. “From Symfony 2.7 & next Tons of new features …

    and keeps counting Better integration with other libraries As powerful as the JMSSerializer!
  8. None
  9. Track B

  10. 1. WHAT’S THAT?

  11. “Serialization is the process of translating data structure 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
  12. “Serialization is the process of translating data structure 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
  13. Object Formated String Basically…

  14. WHICH KIND OF FORMATED STRING? Textual Human readable / editable

    JSON, XML… Binary Smaller, faster Protocol buffers…
  15. WHEN YOU SHOULD USE SERIALIZATION? Send / Receive data thanks

    an API Send / Receive data through messaging system (AMQP) Detect changes (for tests for instance) Store data in database (RDBMS, Elasticsearch, Redis)
  16. WHEN YOU SHOULD USE SERIALIZATION? Send / Receive data thanks

    an API Send / Receive data through messaging system (AMQP) Detect changes (for tests for instance) Store data in database (RDBMS, Elasticsearch, Redis)
  17. Why not use PHP functions? serialize()/unserialize()

  18. Why not use PHP functions? serialize()/unserialize() Powerful but not easily

    configurable
  19. “Here comes the Serializer component!

  20. “Here comes the Serializer component!

  21. 2. WHAT’S GOING ON IN THE SYMFONY SERIALIZER?

  22. 2. Component’s architecture

  23. Normalizers 2. Component’s architecture

  24. Normalizers Encoders 2. Component’s architecture

  25. Normalizers Encoders Serializer 2. Component’s architecture

  26. Let’s talk about Normalizers (and Denormalizers) 2. Component’s architecture >

    Normalizer
  27. OBJECT NORMALIZER (FROM OBJECT TO ARRAY) GetSetMethodNormalizer Uses getters and

    setters PropertyNormalizer Uses Reflection CustomNormalizer Uses $object->normalize(); ObjectNormalizer Uses PropertyAccess Component 2. Component’s architecture > Normalizer
  28. OBJECT NORMALIZER (FROM OBJECT TO ARRAY) GetSetMethodNormalizer Uses getters and

    setters PropertyNormalizer Uses Reflection CustomNormalizer Uses $object->normalize(); ObjectNormalizer Uses PropertyAccess Component Take a look at the other existing Normalizers 2. Component’s architecture > Normalizer
  29. COOL EXTRA NORMALIZER DataUriNormalizer Turns SplFileInfo into data:uri 2. Component’s

    architecture > Normalizer
  30. NORMALIZER USAGE 2. Component’s architecture > Normalizer

  31. DENORMALIZER USAGE 2. Component’s architecture > Normalizer

  32. Let’s talk about Encoders (and Decoders) 2. Component’s architecture >

    Encoder
  33. NATIVE ENCODERS (FROM ARRAY TO YOUR CHOSEN FORMAT) JsonEncoder XmlEncoder

    CsvEncoder YamlEncoder 2. Component’s architecture > Encoder
  34. ENCODER USAGE 2. Component’s architecture > Encoder

  35. DECODER USAGE 2. Component’s architecture > Encoder

  36. Let’s talk about Serializers (and Deserializers) 2. Component’s architecture >

    Serializer
  37. CREATE YOUR SERIALIZER 2. Component’s architecture > Serializer

  38. SERIALIZER USAGE 2. Component’s architecture > Serializer

  39. DESERIALIZER USAGE 2. Component’s architecture > Serializer

  40. 3. USE THE SERIALIZER IN SYMFONY FULL STACK

  41. https://github.com/symfony/symfony-demo 3. Serializer usage

  42. FIRST STEP: ENABLE THE COMPONENT 3. Serializer usage

  43. “Let’s build an API on top of symfony demo app

    3. Serializer usage
  44. “Let’s build an API on top of symfony demo app

    3. Serializer usage
  45. “ It’s all about serialization and deserialization 3. Serializer usage

  46. SERIALIZATION

  47. CREATE A CONTROLLER 3. Serializer usage> Serialization

  48. 3. Serializer usage> Serialization

  49. 3. Serializer usage> Serialization

  50. WHAT WE WANT WHEN SERIALIZED 3. Serializer usage> Serialization

  51. Let’s see how it’s easy! CONFIGURATION: 3. Serializer usage> Serialization

  52. Let’s see how it’s easy! CONFIGURATION: 1. Groups of serialization

    2. Name converter 3. Max depth 4. Add more information to your presentation 3. Serializer usage> Serialization
  53. Groups of serialization CONFIGURATION TIPS 1/4: 3. Serializer usage> Serialization

    > Configuration tips (1/4)
  54. GROUPS OF SERIALIZATION Choose which properties is going to be

    (de)serialized Use cases: For read / write access For security (public, connected user, admin user) For API versionning 3. Serializer usage> Serialization > Configuration tips (1/4)
  55. Configuration in your Entity 3. Serializer usage> Serialization > Configuration

    tips (1/4)
  56. Use your groups for the serialization 3. Serializer usage> Serialization

    > Configuration tips (1/4)
  57. [ { "id": 31, "publishedAt": "2017-03-22T19:10:11+01:00", "summary": "This is the

    summary", "title": "This is the title #31" }, { "id": 32, "publishedAt": "2017-03-21T19:10:11+01:00", "summary": "This is the summary", "title": "This is the title #32" } LET’S SEE THE RESULT 3. Serializer usage> Serialization > Configuration tips (1/4)
  58. Name Converter CONFIGURATION TIPS 2/4: 3. Serializer usage> Serialization >

    Configuration tips (2/4)
  59. 3. Serializer usage> Serialization > Configuration tips (2/4)

  60. [ { "id": 31, "published_at": "2017-03-22T19:10:11+01:00", "summary": "This is the

    summary", "title": "This is the title #31" }, { "id": 32, "published_at": "2017-03-21T19:10:11+01:00", "summary": "This is the summary", "title": "This is the title #32" } LET’S SEE THE RESULT 3. Serializer usage> Serialization > Configuration tips (2/4)
  61. None
  62. None
  63. None
  64. None
  65. Max Depth CONFIGURATION TIPS 3/4: 3. Serializer usage> Serialization >

    Configuration tips (3/4)
  66. 3. Serializer usage> Serialization > Configuration tips (3/4)

  67. None
  68. None
  69. Add more data with Custom Object Normalizer CONFIGURATION TIPS 4/4:

    3. Serializer usage> Serialization > Configuration tips (4/4)
  70. CUSTOM OBJECT NORMALIZER Allows you to tweak the normalization 3.

    Serializer usage> Serialization > Configuration tips (4/4)
  71. CUSTOM OBJECT NORMALIZER Allows you to tweak the normalization 3.

    Serializer usage> Serialization > Configuration tips (4/4)
  72. CUSTOM OBJECT NORMALIZER Allows you to tweak at PHP level

    the normalization Add or remove some data, for instance: computed data (total number of comments, canonical URL…) Hypermedia! 3. Serializer usage> Serialization > Configuration tips (4/4)
  73. 1/3 3. Serializer usage> Serialization > Configuration tips (4/4)

  74. 1/3 3. Serializer usage> Serialization > Configuration tips (4/4)

  75. 2/3 * call the normalize() method, and then do whatever

    you want with the result 3. Serializer usage> Serialization > Configuration tips (4/4)
  76. 3/3 3. Serializer usage> Serialization > Configuration tips (4/4)

  77. LET’S SEE THE RESULT 3. Serializer usage> Serialization > Configuration

    tips (4/4)
  78. ONE LAST COOL THING WITH SERIALIZATION CIRCULAR REFERENCES 3. Serializer

    usage> Serialization > Circular reference
  79. ONE LAST COOL THING WITH SERIALIZATION CIRCULAR REFERENCES 3. Serializer

    usage> Serialization > Circular reference
  80. None
  81. None
  82. USE CASE By default, when serializing the Post object, an

    exception will be thrown because of the circular reference. 1 0..n Comment Post $post Post 3. Serializer usage> Serialization > Circular reference
  83. USE CASE By default, when serializing the Post object, an

    exception will be thrown because of the circular reference. 1 0..n Comment Post $post Post 3. Serializer usage> Serialization > Circular reference
  84. 1/3 3. Serializer usage> Serialization > Circular reference

  85. 2/3 3. Serializer usage> Serialization > Circular reference

  86. 2/3 Before 3.3 From 3.3 3. Serializer usage> Serialization >

    Circular reference
  87. DESERIALIZATION

  88. COMMON WAY OF DESERIALIZING 3. Serializer usage> Deserialization

  89. “Validate your data! 3. Serializer usage> Deserialization > Validation

  90. “Validate your data! 3. Serializer usage> Deserialization > Validation

  91. VALIDATE DATA! 1/2 3. Serializer usage> Deserialization > Validation

  92. VALIDATE DATA! 2/2 3. Serializer usage> Deserialization > Validation

  93. MANAGE THE SERIALISATION OF ConstraintViolationList 3. Serializer usage> Deserialization >

    Validation
  94. MANAGE THE SERIALISATION OF ConstraintViolationList Shipped with 3. Serializer usage>

    Deserialization > Validation
  95. LET’S SEE THE RESULT IF THE DATA ARE NOT VALID

    3. Serializer usage> Deserialization > Validation
  96. LET’S SEE THE RESULT IF THE DATA ARE VALID 3.

    Serializer usage> Deserialization > Validation
  97. DESERIALIZE INTO AN OBJECT 3. Serializer usage> Deserialization > In

    object
  98. DESERIALIZE OF ARRAY 3. Serializer usage> Deserialization > In array

  99. TO SUM UP

  100. TO SUM UP

  101. None
  102. Use groups Create normalizers for each serialized entity Manage circular

    references
  103. Use groups Create normalizers for each serialized entity Manage circular

    references
  104. @catlannister Thank you! * Thanks to @lyrixx