Serialize and deserialize single entity object work properly for me.
It is possible to serialize and deserialize multiple object (array of objects) in this way??
$notifications = $this->getDoctrine()
->getRepository('AppBundle:Notification')
->findAll();
$encoder = new JsonEncoder();
$normalizer = new ObjectNormalizer();
$serializer = new Serializer(array($normalizer), array($encoder));
$jsonContent = $serializer->serialize($notifications, 'json');
return new Response($jsonContent);
And
$response = curl_exec($ch); // my $jsonContent from previous code
$encoder = new JsonEncoder();
$normalizer = new ObjectNormalizer();
$serializer = new Serializer(array($normalizer), array($encoder));
$notifications = $serializer->deserialize($response, Notification::class, 'json');
Then i got:
The property path constructor needs a string or an instance of "Symfony\Component\PropertyAccess\PropertyPath". Got: "integer" 500 Internal Server Error - UnexpectedValueException
I found solution
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Serializer;
$serializer = new Serializer(
array(new GetSetMethodNormalizer(), new ArrayDenormalizer()),
array(new JsonEncoder())
);
$data = ...; // The serialized data from the previous example
$persons = $serializer->deserialize($data, 'Acme\Person[]', 'json');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With