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();
$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;
echo 
'<pre>'.$serialized.'</pre>';
dump($unserialized);

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

echo 
'<b>XML</b>' PHP_EOL;
echo 
'<pre>'.htmlentities($serialized).'</pre>';
dump($unserialized);
Result
JSON
{"key":"value","foo":"bar"}
^ array:2 [
"key" => "value"
"foo" => "bar"
]

XML
<?xml version="1.0"?>
<response><key>value</key><foo>bar</foo></response>
^ array:2 [
"key" => "value"
"foo" => "bar"
]
Used Versions
PHP 8.0, Laminas MVC 3.2, Symfony 5.2, Laravel 8.28, PHPUnit 9.5, Doctrine ORM 2.8