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

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

    View full-size slide

  3. 3
    Serialization ?

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

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

    View full-size slide

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

    View full-size slide

  8. symfony/serializer
    8

    View full-size slide

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

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

    View full-size slide

  11. Architecture

    View full-size slide

  12. 15
    And Decoders
    Encoders

    View full-size slide

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

    View full-size slide

  14. Encoder usage
    17

    View full-size slide

  15. Decoder usage
    18

    View full-size slide

  16. 20
    And denormalizers
    Normalizers

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

  20. Normalizer usage
    24

    View full-size slide

  21. Denormalizer usage
    25

    View full-size slide

  22. 27
    And deserializer
    Serializer

    View full-size slide

  23. Serializer
    28

    View full-size slide

  24. Serializer Usage
    29

    View full-size slide

  25. Deserializer Usage
    30

    View full-size slide

  26. symfony/symfony-demo
    32

    View full-size slide

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

    View full-size slide

  28. Enable the serializer
    34

    View full-size slide

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

    View full-size slide

  30. Create a Controller
    36

    View full-size slide

  31. Configuration
    Groups
    38

    View full-size slide

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

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

  34. Groups
    42
    Context

    View full-size slide

  35. Configuration
    MaxDepth
    43

    View full-size slide

  36. Configuration
    NameConverter
    45

    View full-size slide

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

  38. Try it again
    47

    View full-size slide

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

    View full-size slide

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

  41. PostNormalizer
    50

    View full-size slide

  42. PostNormalizer
    51

    View full-size slide

  43. Custom Object
    Normalizer
    52

    View full-size slide

  44. Try it again
    53

    View full-size slide

  45. 54
    Circular Reference

    View full-size slide

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

  47. Circular Reference
    56

    View full-size slide

  48. Circular Reference
    57

    View full-size slide

  49. Circular Reference
    SF 3.3
    58

    View full-size slide

  50. Circular Reference
    < SF 3.2
    59

    View full-size slide

  51. Deserialization
    Aka: How to use user data
    60

    View full-size slide

  52. Create a Controller
    61

    View full-size slide

  53. Create a Controller
    62

    View full-size slide

  54. ConstraintViolation
    ListNormalizer
    63

    View full-size slide

  55. Send invalid data
    64

    View full-size slide

  56. Send valid data
    65

    View full-size slide

  57. Deserialization
    Into an Object
    66

    View full-size slide

  58. Into an Object
    67

    View full-size slide

  59. Deserialization
    With some relations
    68

    View full-size slide

  60. Post Object
    69

    View full-size slide

  61. Enable the property
    info component
    70

    View full-size slide

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

  63. Deserialization
    Of array
    73

    View full-size slide

  64. The controller
    74

    View full-size slide

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

    View full-size slide

  66. Any questions ?
    @lyrixx
    Thanks!
    78

    View full-size slide