Skip to content

Instantly share code, notes, and snippets.

@dunglas
Created December 14, 2014 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dunglas/6e6a99626ddb9ab3fee4 to your computer and use it in GitHub Desktop.
Save dunglas/6e6a99626ddb9ab3fee4 to your computer and use it in GitHub Desktop.
Symfony's Serializer null handling
<?php
<<<CONFIG
packages:
- "symfony/serializer: ~2.6"
CONFIG;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new GetSetMethodNormalizer());
$serializer = new Serializer($normalizers, $encoders);
class T
{
public function getA()
{
return null;
}
}
echo $serializer->serialize(new T(), 'json') . PHP_EOL;
// Output: {"a":null}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment