Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
is it possible to use ArrayDenormalizer, ObjectNormalizer and GetSetMethodNormalizer together?
<?php
// composer require symfony/serializer symfony/property-info symfony/property-access phpdocumentor/reflection-docblock
require __DIR__ . '/vendor/autoload.php';
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class Model
{
/**
* @var string
*/
public $name;
/**
* @var bool
*/
public $bool;
public function setBool(string $bool): self
{
$this->bool = (bool) $bool;
return $this;
}
}
$extractor = new PropertyInfoExtractor(
[],
[new PhpDocExtractor()]
);
$serializer = new Serializer(
[
new ArrayDenormalizer(),
new ObjectNormalizer(null, null, null, $extractor),
new GetSetMethodNormalizer(),
],
[
new JsonEncoder(),
]
);
$json = <<<'JSON'
{
"name": "Test",
"bool":"1"
}
JSON;
$object = $serializer->deserialize(
$json,
Model::class,
'json'
);
var_dump($object);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment