Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony3 deserialize multiple entity objects

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

like image 439
Jarek Kowol Avatar asked Dec 04 '16 21:12

Jarek Kowol


1 Answers

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');
like image 142
Jarek Kowol Avatar answered Oct 29 '22 13:10

Jarek Kowol