I want to deserialize something like this:
[
{ "id": 42 },
{ "id": 43 }
]
Any idea how to do this?
I want to deserialize something like this:
[
{ "id": 42 },
{ "id": 43 }
]
Any idea how to do this?
It would be
$serializer->deserialize($json, 'array<T>', 'json')
where T
is the name of the class with the id
property.
Let's say you have a class foo, that has an attribute with an array of bar objects.
In your foo class use JMS\Serializer\Annotation\Type as Type;
and annotate the attribute like this:
use JMS\Serializer\Annotation\Type as Type;
class foo {
/**
*
* @Type("array<int,Namespace\To\Class\Bar>")
* private $bars = array();
*/
}
JMS Serializer will serialize/deserialize the contents of foo automagically. This use of @Type should be utilized for all attributes of any classes you will be serializing/deserializing.
This also works in situations where you are using string keys (ie. associative array structure). Just substitute "string" for int.
@Type("array<string,Namespace\To\Class\Bar>")