Slide 1

Slide 1 text

DISCOVER THE SERIALIZER COMPONENT Sarah Khalil @saro0h

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

A BIT OF HISTORY FIRST

Slide 4

Slide 4 text

“Before 2.7:

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Track B

Slide 10

Slide 10 text

1. WHAT’S THAT?

Slide 11

Slide 11 text

“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

Slide 12

Slide 12 text

“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

Slide 13

Slide 13 text

Object Formated String Basically…

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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)

Slide 16

Slide 16 text

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)

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

“Here comes the Serializer component!

Slide 20

Slide 20 text

“Here comes the Serializer component!

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

2. Component’s architecture

Slide 23

Slide 23 text

Normalizers 2. Component’s architecture

Slide 24

Slide 24 text

Normalizers Encoders 2. Component’s architecture

Slide 25

Slide 25 text

Normalizers Encoders Serializer 2. Component’s architecture

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

NORMALIZER USAGE 2. Component’s architecture > Normalizer

Slide 31

Slide 31 text

DENORMALIZER USAGE 2. Component’s architecture > Normalizer

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

ENCODER USAGE 2. Component’s architecture > Encoder

Slide 35

Slide 35 text

DECODER USAGE 2. Component’s architecture > Encoder

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

CREATE YOUR SERIALIZER 2. Component’s architecture > Serializer

Slide 38

Slide 38 text

SERIALIZER USAGE 2. Component’s architecture > Serializer

Slide 39

Slide 39 text

DESERIALIZER USAGE 2. Component’s architecture > Serializer

Slide 40

Slide 40 text

3. USE THE SERIALIZER IN SYMFONY FULL STACK

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

FIRST STEP: ENABLE THE COMPONENT 3. Serializer usage

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

SERIALIZATION

Slide 47

Slide 47 text

CREATE A CONTROLLER 3. Serializer usage> Serialization

Slide 48

Slide 48 text

3. Serializer usage> Serialization

Slide 49

Slide 49 text

3. Serializer usage> Serialization

Slide 50

Slide 50 text

WHAT WE WANT WHEN SERIALIZED 3. Serializer usage> Serialization

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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)

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

[ { "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)

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

[ { "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)

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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)

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

DESERIALIZATION

Slide 88

Slide 88 text

COMMON WAY OF DESERIALIZING 3. Serializer usage> Deserialization

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

TO SUM UP

Slide 100

Slide 100 text

TO SUM UP

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

Use groups Create normalizers for each serialized entity Manage circular references

Slide 103

Slide 103 text

Use groups Create normalizers for each serialized entity Manage circular references

Slide 104

Slide 104 text

@catlannister Thank you! * Thanks to @lyrixx