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

Symfony Live 2017 - Serializer

Symfony Live 2017 - Serializer

Grégoire Pineau

March 31, 2017
Tweet

More Decks by Grégoire Pineau

Other Decks in Technology

Transcript

  1. SymfonyLive
    Paris 2017
    The Serializer
    Component

    View Slide

  2. I am Grégoire Pineau
    DevOps @blackfireio / @sensiolabs
    @symfony Core Contributor
    Github / Twitter : @lyrixx
    Hello!
    2

    View Slide

  3. 3
    Serialization ?

    View Slide


  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

    View Slide

  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

    View Slide

  6. Why?
    ◉ PHP serialize / unserialize functions are powerful but:
    ○ Not interoperable
    ○ Not configurable
    6

    View Slide

  7. Different formats
    ◉ Binary:
    ○ Faster
    ○ Smaller
    ○ Ex: protobuf, Apache Thrift, …
    ◉ Textual
    ○ Human readable / editable
    ○ Ex: CSV, JSON, XML, YAML, ... 7

    View Slide

  8. symfony/serializer
    8

    View Slide

  9. History

    View Slide

  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

    View Slide

  11. History
    11

    View Slide

  12. Symfony 2.7 & next
    ◉ Tons of new features
    ◉ … and keeps counting
    ◉ Better integration with others lib
    ◉ Simpler than JMSSerializer (?)
    12

    View Slide

  13. Architecture

    View Slide

  14. 14

    View Slide

  15. 15
    And Decoders
    Encoders

    View Slide

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

    View Slide

  17. Encoder usage
    17

    View Slide

  18. Decoder usage
    18

    View Slide

  19. 19

    View Slide

  20. 20
    And denormalizers
    Normalizers

    View Slide

  21. Normalizers
    AbstractNormalizer, AbstractObjectNormalizer, ArrayDenormalizer,
    ContextAwareDenormalizerInterface,
    ContextAwareNormalizerInterface, CustomNormalizer,
    DataUriNormalizer, DateTimeNormalizer, DenormalizableInterface,
    DenormalizerAwareInterface, DenormalizerAwareTrait,
    DenormalizerInterface, GetSetMethodNormalizer,
    JsonSerializableNormalizer, NormalizableInterface,
    NormalizerAwareInterface, NormalizerAwareTrait, NormalizerInterface,
    ObjectNormalizer, PropertyNormalizer, SerializerAwareNormalizer
    21

    View Slide

  22. Object Normalizers
    GetSetMethodNormalizer Uses getters and setters
    PropertyNormalizer Uses Reflection
    CustomNormalizer Uses $object->normalize();
    ObjectNormalizer Uses PropertyAccess Component
    22

    View Slide

  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

    View Slide

  24. Normalizer usage
    24

    View Slide

  25. Denormalizer usage
    25

    View Slide

  26. 26

    View Slide

  27. 27
    And deserializer
    Serializer

    View Slide

  28. Serializer
    28

    View Slide

  29. Serializer Usage
    29

    View Slide

  30. Deserializer Usage
    30

    View Slide

  31. 31

    View Slide

  32. symfony/symfony-demo
    32

    View Slide

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

    View Slide

  34. Enable the serializer
    34

    View Slide

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

    View Slide

  36. Create a Controller
    36

    View Slide

  37. Try it
    37

    View Slide

  38. Configuration
    Groups
    38

    View Slide

  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

    View Slide

  40. Groups
    40

    View Slide

  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

    View Slide

  42. Groups
    42
    Context

    View Slide

  43. Configuration
    MaxDepth
    43

    View Slide

  44. MaxDepth
    44

    View Slide

  45. Configuration
    NameConverter
    45

    View Slide

  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

    View Slide

  47. Try it again
    47

    View Slide

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

    View Slide

  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

    View Slide

  50. PostNormalizer
    50

    View Slide

  51. PostNormalizer
    51

    View Slide

  52. Custom Object
    Normalizer
    52

    View Slide

  53. Try it again
    53

    View Slide

  54. 54
    Circular Reference

    View Slide

  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

    View Slide

  56. Circular Reference
    56

    View Slide

  57. Circular Reference
    57

    View Slide

  58. Circular Reference
    SF 3.3
    58

    View Slide

  59. Circular Reference
    < SF 3.2
    59

    View Slide

  60. Deserialization
    Aka: How to use user data
    60

    View Slide

  61. Create a Controller
    61

    View Slide

  62. Create a Controller
    62

    View Slide

  63. ConstraintViolation
    ListNormalizer
    63

    View Slide

  64. Send invalid data
    64

    View Slide

  65. Send valid data
    65

    View Slide

  66. Deserialization
    Into an Object
    66

    View Slide

  67. Into an Object
    67

    View Slide

  68. Deserialization
    With some relations
    68

    View Slide

  69. Post Object
    69

    View Slide

  70. Enable the property
    info component
    70

    View Slide

  71. Try it!
    71

    View Slide

  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

    View Slide

  73. Deserialization
    Of array
    73

    View Slide

  74. The controller
    74

    View Slide

  75. Summary

    View Slide

  76. 76

    View Slide

  77. Summary
    77
    ◉ Use Groups
    ◉ Create a normalizer for each serialized entity
    ○ Service tag: ‘serializer.normalizer’
    ◉ Create a circular reference handler

    View Slide

  78. Any questions ?
    @lyrixx
    Thanks!
    78

    View Slide