5

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

1
  • Will sounds stupid but the error message does tell you everything... Your property path constructor is waiting a an instance of PropertyPath, which is a child of PropertyAccess. But, your constructor get an Integer as parameter, which is obviously wrong. Can you add your construtor and everything related to it in your question plz? Commented Dec 4, 2016 at 23:36

1 Answer 1

5

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');
Sign up to request clarification or add additional context in comments.

1 Comment

Hello for people still hoping to use Symfony Serializer (and most of all deserializer), I found : medium.com/@maartendeboer/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.