-
-
Notifications
You must be signed in to change notification settings - Fork 585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle classes that implement collections (e.g. ArrayObject) and properties #137
Comments
I have the same issue |
Same issue here, I will try to search for a solution. My best bet would be to use a custom handler for this. Will provide some code when I got a working solution. |
Here is a working example. If you use symfony2 add this as a service with the tag 'jms_serializer.subscribing_handler'. More info about JMS handlers: http://jmsyst.com/libs/serializer/master/handlers class ArrayObjectHandler implements SubscribingHandlerInterface
{
/**
* @return array
*/
public static function getSubscribingMethods()
{
$methods = array();
$formats = array('json', 'xml', 'yml');
$types = array(
'ArrayObject'
);
foreach ($types as $type) {
foreach ($formats as $format) {
$methods[] = array(
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
'type' => $type,
'format' => $format,
'method' => 'serializeArrayObject',
);
}
}
return $methods;
}
/**
* @param VisitorInterface $visitor
* @param \ArrayObject $data
* @param array $type
* @param Context $context
* @return mixed
*/
public function serializeArrayObject(VisitorInterface $visitor, \ArrayObject $data, array $type, Context $context)
{
$type['name'] = 'array';
return $visitor->visitArray($data->getArrayCopy(), $type, $context);
}
} |
@steffenbrem Your implementation will not serialize the |
This has to be solved with a custom handler (since the usecase explained here is a custom extension of the array object class) |
I have a use case where a class realizes a collection and holds properties with meta data in addition. Without any additional config, JMS Serializer only takes care for the properties. I could get it working to serialize the collection part, too, by defining
getArrayCopy()
as an accessor for a virtual property. However, since virtual properties do not support deserialization, the collection would be lost.Find a Gist to illustrate what I desire to do here: https://gist.github.com/tobyS/6397023
The text was updated successfully, but these errors were encountered: