schmittjoh/JMSSerializerBundle — Issue #48: serializing an array of objects using SerializationHandlerInterface
Author: lsmith77
Created: 2011-11-23 15:26:44+00:00
State: open

We are porting a current API from using the core NormalizeableInterface to SerializationHandlerInterface.
Currently for JSON we end up with something like:
http://screencast.com/t/5j8IiEXGmjsS

And for XML something like:
http://screencast.com/t/j0KRbKTnIJ63

With the following code we managed to handle JSON, but for XML we seem to have to set the entry name somehow. So we looked into setting this, but found that we have to do that on a property metadata instance, but we are not sure what property, since we have an array of objects.

public function serialize(VisitorInterface $visitor, $data, $type, &$visited)
{
$visited = true;
if ($visitor instanceof JsonSerializationVisitor) {
return $this->toArray();
}

if ($visitor instanceof XmlSerializationVisitor) {
//$md = new PropertyMetadata();
//$md->xmlEntryName = 'article';
//$visitor->setCurrentMetadata($md);
$visitor->visitArray($this->toArray(), $type);
return;
}
}