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 full-size 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 full-size slide

  3. A BIT OF HISTORY FIRST

    View full-size slide

  4. “Before 2.7:

    View full-size slide

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

    View full-size 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 full-size slide

  7. 1. WHAT’S THAT?

    View full-size slide

  8. “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 full-size slide

  9. “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 full-size slide

  10. Object
    Formated
    String
    Basically…

    View full-size slide

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

    View full-size slide

  12. 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 full-size slide

  13. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  16. “Here comes the Serializer component!

    View full-size slide

  17. “Here comes the Serializer component!

    View full-size slide

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

    View full-size slide

  19. 2. Component’s architecture

    View full-size slide

  20. Normalizers
    2. Component’s architecture

    View full-size slide

  21. Normalizers
    Encoders
    2. Component’s architecture

    View full-size slide

  22. Normalizers
    Encoders
    Serializer
    2. Component’s architecture

    View full-size slide

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

    View full-size slide

  24. 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 full-size slide

  25. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  37. 3. USE THE SERIALIZER IN
    SYMFONY FULL STACK

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide


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

    View full-size slide

  43. SERIALIZATION

    View full-size slide

  44. CREATE A CONTROLLER
    3. Serializer usage> Serialization

    View full-size slide

  45. 3. Serializer usage> Serialization

    View full-size slide

  46. 3. Serializer usage> Serialization

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  49. 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 full-size slide

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

    View full-size slide

  51. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  54. [
    {
    "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 full-size slide

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

    View full-size slide

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

    View full-size slide

  57. [
    {
    "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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  63. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  71. 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 full-size slide

  72. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  76. DESERIALIZATION

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  83. MANAGE THE SERIALISATION OF ConstraintViolationList
    Shipped with

    3. Serializer usage> Deserialization > Validation

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide