Last active
August 13, 2019 07:54
-
-
Save lsv/1fd1deaa3ccb4e6cbf1931fb5cf5a94e to your computer and use it in GitHub Desktop.
is it possible to use ArrayDenormalizer, ObjectNormalizer and GetSetMethodNormalizer together?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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