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"}]
James Dunn
  • 8,064
  • 13
  • 53
  • 87
mrMantir
  • 2,285
  • 1
  • 17
  • 20

1 Answers1

43

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.

mrMantir
  • 2,285
  • 1
  • 17
  • 20
  • 1
    Where did you find this solution? – tolgap Oct 21 '12 at 13:26
  • 3
    by experiment only. No docs found unfortunately but looks like deserialize method can take as type argument same strings as defined for example here: [types](http://jmsyst.com/bundles/JMSSerializerBundle/master/reference/annotations#type) – mrMantir Oct 23 '12 at 08:30
  • just what I was looking for. thanks for finding the solution! – Casey Feb 05 '13 at 08:05
  • 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. – Doug Jul 04 '14 at 08:50
  • You saved me a lot of work this solution works perfectly thanks – Robert Jul 22 '16 at 07:53
  • I'm glad it is still helpful :) – mrMantir Jul 22 '16 at 08:55