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

Discover the Serializer component

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.

Sarah KHALIL

September 22, 2017
Tweet

More Decks by Sarah KHALIL

Other Decks in Technology

Transcript

  1. DISCOVER THE SERIALIZER COMPONENT
    Sarah Khalil
    @saro0h

    View Slide

  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

    View Slide

  3. A BIT OF HISTORY FIRST

    View Slide

  4. “Before 2.7:

    View Slide

  5. View Slide

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

    View Slide

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

    View Slide

  8. View Slide

  9. Track B

    View Slide

  10. 1. WHAT’S THAT?

    View Slide

  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

    View Slide

  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

    View Slide

  13. Object
    Formated
    String
    Basically…

    View Slide

  14. WHICH KIND OF FORMATED STRING?
    Textual
    Human readable / editable
    JSON, XML…
    Binary
    Smaller, faster
    Protocol buffers…

    View Slide

  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)

    View Slide

  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)

    View Slide

  17. Why not use
    PHP functions?
    serialize()/unserialize()

    View Slide

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

    View Slide

  19. “Here comes the Serializer component!

    View Slide

  20. “Here comes the Serializer component!

    View Slide

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

    View Slide

  22. 2. Component’s architecture

    View Slide

  23. Normalizers
    2. Component’s architecture

    View Slide

  24. Normalizers
    Encoders
    2. Component’s architecture

    View Slide

  25. Normalizers
    Encoders
    Serializer
    2. Component’s architecture

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  29. COOL EXTRA NORMALIZER
    DataUriNormalizer Turns SplFileInfo into data:uri
    2. Component’s architecture > Normalizer

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  33. NATIVE ENCODERS (FROM ARRAY TO YOUR CHOSEN FORMAT)
    JsonEncoder
    XmlEncoder
    CsvEncoder
    YamlEncoder
    2. Component’s architecture > Encoder

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  40. 3. USE THE SERIALIZER IN
    SYMFONY FULL STACK

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide


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

    View Slide

  46. SERIALIZATION

    View Slide

  47. CREATE A CONTROLLER
    3. Serializer usage> Serialization

    View Slide

  48. 3. Serializer usage> Serialization

    View Slide

  49. 3. Serializer usage> Serialization

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  53. Groups of serialization
    CONFIGURATION TIPS 1/4:
    3. Serializer usage> Serialization > Configuration tips (1/4)

    View Slide

  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)

    View Slide

  55. Configuration in your Entity
    3. Serializer usage> Serialization > Configuration tips (1/4)

    View Slide

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

    View Slide

  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)

    View Slide

  58. Name Converter
    CONFIGURATION TIPS 2/4:
    3. Serializer usage> Serialization > Configuration tips (2/4)

    View Slide

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

    View Slide

  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)

    View Slide

  61. View Slide

  62. View Slide

  63. View Slide

  64. View Slide

  65. Max Depth
    CONFIGURATION TIPS 3/4:
    3. Serializer usage> Serialization > Configuration tips (3/4)

    View Slide

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

    View Slide

  67. View Slide

  68. View Slide

  69. Add more data with Custom
    Object Normalizer
    CONFIGURATION TIPS 4/4:
    3. Serializer usage> Serialization > Configuration tips (4/4)

    View Slide

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

    View Slide

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

    View Slide

  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)

    View Slide

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

    View Slide

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

    View Slide

  75. 2/3
    * call the normalize() method, and then do whatever you want with the result
    3. Serializer usage> Serialization > Configuration tips (4/4)

    View Slide

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

    View Slide

  77. LET’S SEE THE RESULT
    3. Serializer usage> Serialization > Configuration tips (4/4)

    View Slide

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

    View Slide

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

    View Slide

  80. View Slide

  81. View Slide

  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

    View Slide

  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

    View Slide

  84. 1/3
    3. Serializer usage> Serialization > Circular reference

    View Slide

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

    View Slide

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

    View Slide

  87. DESERIALIZATION

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  94. MANAGE THE SERIALISATION OF ConstraintViolationList
    Shipped with

    3. Serializer usage> Deserialization > Validation

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  99. TO SUM UP

    View Slide

  100. TO SUM UP

    View Slide

  101. View Slide

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

    View Slide

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

    View Slide

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

    View Slide