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. 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
  2. “From Symfony 2.7 & next Tons of new features …

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

    and keeps counting Better integration with other libraries As powerful as the JMSSerializer!
  4. “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
  5. “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
  6. WHICH KIND OF FORMATED STRING? Textual Human readable / editable

    JSON, XML… Binary Smaller, faster Protocol buffers…
  7. 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)
  8. 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)
  9. 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
  10. 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
  11. NATIVE ENCODERS (FROM ARRAY TO YOUR CHOSEN FORMAT) JsonEncoder XmlEncoder

    CsvEncoder YamlEncoder 2. Component’s architecture > Encoder
  12. 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
  13. 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)
  14. [ { "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)
  15. [ { "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)
  16. Add more data with Custom Object Normalizer CONFIGURATION TIPS 4/4:

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

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

    Serializer usage> Serialization > Configuration tips (4/4)
  19. 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)
  20. 2/3 * call the normalize() method, and then do whatever

    you want with the result 3. Serializer usage> Serialization > Configuration tips (4/4)
  21. 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
  22. 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
  23. LET’S SEE THE RESULT IF THE DATA ARE NOT VALID

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

    Serializer usage> Deserialization > Validation