Serialization to Yaml, XML or Json with JMS Serializer

Code
<?php

use JMS\Serializer\SerializerBuilder;
use 
JMS\Serializer\SerializationContext;
use 
JMS\Serializer\Annotation\XmlKeyValuePairs;

$source = [
    
'key' => 'value',
    
'numbers' => [1,2,3,4],
    
'sub' => [
        
'key1' => 'val1',
        
'key2' => 'val2',
    ],
];

$serializer = (new SerializerBuilder())
    ->
build();

echo 
'<pre>' $serializer->serialize($source'json') . '</pre>';

echo 
'<pre>' $serializer->serialize($source'yml') . '</pre>';

// xml need more configuration. This is just default
echo '<pre>' htmlentities(
    
$serializer->serialize($source'xml')
) . 
'</pre>';
Result
{"key":"value","numbers":[1,2,3,4],"sub":{"key1":"val1","key2":"val2"}}
key: value
numbers:
    - 1
    - 2
    - 3
    - 4
sub:
    key1: val1
    key2: val2
<?xml version="1.0" encoding="UTF-8"?>
<result>
  <entry><![CDATA[value]]></entry>
  <entry>
    <entry>1</entry>
    <entry>2</entry>
    <entry>3</entry>
    <entry>4</entry>
  </entry>
  <entry>
    <entry><![CDATA[val1]]></entry>
    <entry><![CDATA[val2]]></entry>
  </entry>
</result>
Used Versions
PHP 7.1, Zend 3.0, Symfony 3.2, Laravel 5.2, PHPUnit 5.6, Doctrine 2.6