Slide 1

Slide 1 text

SymfonyLive Paris 2017 The Serializer Component

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

3 Serialization ?

Slide 4

Slide 4 text

“ 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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

symfony/serializer 8

Slide 9

Slide 9 text

History

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

History 11

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Architecture

Slide 14

Slide 14 text

14

Slide 15

Slide 15 text

15 And Decoders Encoders

Slide 16

Slide 16 text

Encoders ◉ CsvEncoder ◉ JsonEncoder ◉ XmlEncoder ◉ YamlEncoder 16

Slide 17

Slide 17 text

Encoder usage 17

Slide 18

Slide 18 text

Decoder usage 18

Slide 19

Slide 19 text

19

Slide 20

Slide 20 text

20 And denormalizers Normalizers

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Normalizer usage 24

Slide 25

Slide 25 text

Denormalizer usage 25

Slide 26

Slide 26 text

26

Slide 27

Slide 27 text

27 And deserializer Serializer

Slide 28

Slide 28 text

Serializer 28

Slide 29

Slide 29 text

Serializer Usage 29

Slide 30

Slide 30 text

Deserializer Usage 30

Slide 31

Slide 31 text

31

Slide 32

Slide 32 text

symfony/symfony-demo 32

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Enable the serializer 34

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Create a Controller 36

Slide 37

Slide 37 text

Try it 37

Slide 38

Slide 38 text

Configuration Groups 38

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Groups 40

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

Groups 42 Context

Slide 43

Slide 43 text

Configuration MaxDepth 43

Slide 44

Slide 44 text

MaxDepth 44

Slide 45

Slide 45 text

Configuration NameConverter 45

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

Try it again 47

Slide 48

Slide 48 text

Add more data ... … to the representation 48

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

PostNormalizer 50

Slide 51

Slide 51 text

PostNormalizer 51

Slide 52

Slide 52 text

Custom Object Normalizer 52

Slide 53

Slide 53 text

Try it again 53

Slide 54

Slide 54 text

54 Circular Reference

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

Circular Reference 56

Slide 57

Slide 57 text

Circular Reference 57

Slide 58

Slide 58 text

Circular Reference SF 3.3 58

Slide 59

Slide 59 text

Circular Reference < SF 3.2 59

Slide 60

Slide 60 text

Deserialization Aka: How to use user data 60

Slide 61

Slide 61 text

Create a Controller 61

Slide 62

Slide 62 text

Create a Controller 62

Slide 63

Slide 63 text

ConstraintViolation ListNormalizer 63

Slide 64

Slide 64 text

Send invalid data 64

Slide 65

Slide 65 text

Send valid data 65

Slide 66

Slide 66 text

Deserialization Into an Object 66

Slide 67

Slide 67 text

Into an Object 67

Slide 68

Slide 68 text

Deserialization With some relations 68

Slide 69

Slide 69 text

Post Object 69

Slide 70

Slide 70 text

Enable the property info component 70

Slide 71

Slide 71 text

Try it! 71

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

Deserialization Of array 73

Slide 74

Slide 74 text

The controller 74

Slide 75

Slide 75 text

Summary

Slide 76

Slide 76 text

76

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

Any questions ? @lyrixx Thanks! 78