JMSSerializer deserializing collection
Asked Answered
M

1

18

I have a problem with deserializing serialized collection of doctrine collections. Couldn't find docs about that and any topics and I'm new with JMSSerializer. When I try deserialize with:

$collection = $serializer->deserialize($jsonData,'Doctrine\Common\Collections\ArrayCollection','json');

$collection is empty

When I set to null instead of class name I have assoc array on result. Is there an elegant way to deserialize that json?

EDIT: Sorry. Here is serialized collection:

[{"id":88,"name":"Poland","created_at":"2012-09-28T11:59:06+0000"},{"id":90,"name":"Great Britain","created_at":"2012-09-28T11:59:06+0000"}]
Matsumoto answered 1/10, 2012 at 19:20 Comment(1)
can you paste the json of the serialized ArrayCollection to the question?Almsman
M
42

Hah! Found what I done wrong :) I gave to deserialize method wrong type. Should be:

$serializer->deserialize($jsonData, 'ArrayCollection<EntityName>', 'json');

and it gave me beatiful array of entities.

Matsumoto answered 2/10, 2012 at 8:37 Comment(6)
Where did you find this solution?Gilletta
by experiment only. No docs found unfortunately but looks like deserialize method can take as type argument same strings as defined for example here: typesMatsumoto
just what I was looking for. thanks for finding the solution!Hydrargyrum
Sorry to bring alive an old question, however I was wondering if this is done inside the controller or the view? 2 years on and the docs are still somewhat lacking.Yunyunfei
You saved me a lot of work this solution works perfectly thanksLode
I'm glad it is still helpful :)Matsumoto

© 2022 - 2024 — McMap. All rights reserved.