Skip to content

Latest commit

 

History

History
55 lines (34 loc) · 2.16 KB

1-serialization.md

File metadata and controls

55 lines (34 loc) · 2.16 KB

1 - Serialization

Serialization is the process of transforming a graph of objects into a structured data format (XML, YAML, JSON but also binary).

1.1 - JMSSerializerBundle

Serialization is brought to you by the JMSSerializerBundle bundle, for free ;-)

Task: Start by modifying the DefaultController class to use the jms_serializer service and return either some XML, HTML, or JSON.

Tip: If there is one thing to retain, it is that serialization takes the exact same data set as input, but outputs it in different formats.

1.2 - JMSSerializerBundle + FOSRestBundle = ♥

A small Behat test suite is provided:

$ bin/behat features/basic-serialization.feature

Tip: Always cover your code by test. If you don't feel good enough with unit testing, functional testing might be an option as it is often easier to understand.

The FOSRestBundle integrates with the JMSSerializerBundle, and provides the same feature through the concept of "views".

Task: Configure the FOSRestBundle to leverage the View layer.

Tip: FOSRestBundle leverages the JMSSerializerBundle for the "serialization" part. If you only use this feature, no need to use the FOSRestBundle. However, this bundle provides many interesting features that we are going to cover in the following.

1.3 - Leveraging JMSSerializerBundle

Task: Configure the XML output to get the following document:

<hello>
    <name><![CDATA[ will ]]></name>
</hello>

Tip: Your response does not have to be the mirror of your database schema. When building an API, you should think about the response first, and then about how to store data.


Back to the index  | Next