Serializing data in PHP: A simple primer on the JMS Serializer and FoS Rest
By: Merlin Carter
Tags: Serialization, Rest Api, Symfony, API, PHP

Are you creating REST APIs in Symfony and trying to decide what bundle to use? Are you wondering how you’ll serialize and deserialize data? Whelp, we created this article to help you.

What you’ll a need to start

If you want to try out this tutorial, you’ll might need to install a few things. it It shouldn’t take any more than 20mins, even if you have you install every item in this list,

PHP 7.2.5+

Composer for installing bundles.

The latest Symfony CLI for bootstrapping our project.

The PHP extensions that Symfony requires.

Most of these extensions are already included in later versions of PHP, so you could first run the terminal command ‘symfony check:requirements’ to see if you need to install anything extra.

Most of these extensions are already included in later versions of PHP, so you could first run the terminal command ‘symfony check:requirements’ to see if you need to install anything extra. JetBrains PhpStorm for generating code.

The Postman API Client for making POST requests.

We’ll also be using the following bundles: Maker, FOSRest, JMS Serializer, and FrameworkExtra, but we’ll show you how to install those in the tutorial.

And of course, you’ll need a basic knowledge of object-oriented programming in PHP.

What we’ll be doing

We’re going to create a REST endpoint which accepts a JSON payload. For the payload, we’ll send the basic properties of a postal address. Then, we are going to serialize the address details into an object. Finally, we’ll create a response which deserializes the object back into another format.

We’ll be using extremely simple examples so it won’t take too long

Why are you using <Technology X> and not <Technology Y>?

Here at Project A, we’re like to use Symfony. As is the case with a lot of our projects, we tend to use older battle-tested technologies rather than the hottest, up-to-the-minute tech. There tends to be more “community documentation” (my euphemism for answers on Stack Overflow) for stuff that’s been around a while.

That’s why we’re going to show you how to create a REST endpoint with the FoS Rest Bundle and serialize some data with the JMS Serializer.

These two bundles have been partners for many years. They’re like Siegfried and Roy…or Rick and Morty. They’re very often used together (although you can also use FOS Rest with Symfony’s built-in serializer). Mostly because JMS is a more sophisticated serializer, and works well with FoS REST — which is natural, since Johannes Schmitt, one of the early contributors to FoS Rest (FoS is “Friends of Symfony”), created the JMS Serializer.

There is also a newer, more comprehensive option for creating APIs, but I’ll talk about that at the end of the tutorial.

Here are the major steps:

Create an empty skeleton project.

We’ll use the Symfony CLI to bootstrap the folder structure and required files, then install a couple of extra bundles. Create a basic REST endpoint and configure it to accept POST requests.

We’ll use the Maker bundle to bootstrap a controller, map it to an endpoint address, start a server, then send an empty request to the endpoint just to make sure it works. Set up the serialization.

We’ll create an “Address” class, import the JMS Serializer bundle and annotate each address property with serialization instructions. Make a POST request with a JSON payload.

We’ll send a POST request containing our address JSON payload and try to get a response back that contains the address as XML. Make our code more concise.

We’ll add the FOSRest and FrameworkExtra bundles so that we can use the ParamConverter — which simplifies the serialization process.

So without further ado, let’s get started.