Symfony Serializer

Code
<?php

use Symfony\Component\Serializer\Serializer;
use 
Symfony\Component\Serializer\Encoder\JsonEncoder;
use 
Symfony\Component\Serializer\Encoder\XmlEncoder;
use 
Symfony\Component\Serializer\Normalizer\CustomNormalizer;

$jsonEncoder = new JsonEncoder();
$xmlEncoder = new XmlEncoder('rootNode');
$normalizer = new CustomNormalizer();

$serializer = new Serializer([$normalizer], [$jsonEncoder$xmlEncoder]);

$array = ['key' => 'value''foo' => 'bar'];

$serialized $serializer->encode($array'json');
$unserialized $serializer->decode($serialized'json');

echo 
'<b>JSON</b>' PHP_EOL;
var_dump($serialized$unserialized);

$serialized $serializer->encode($array'xml');
$unserialized $serializer->decode($serialized'xml');

echo 
'<b>XML</b>' PHP_EOL;
var_dump(htmlentities($serialized), $unserialized);
Result
JSON
string(27) "{"key":"value","foo":"bar"}"
array(2) {
["key"]=>
string(5) "value"
["foo"]=>
string(3) "bar"
}
XML
string(126) "<?xml version="1.0"?>
<rootNode><key>value</key><foo>bar</foo></rootNode>
"
array(2) {
["key"]=>
string(5) "value"
["foo"]=>
string(3) "bar"
}
Used Versions
PHP 7.1, Zend 3.0, Symfony 3.2, Laravel 5.2, PHPUnit 5.6, Doctrine 2.6