Skip to content
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

Closed
tobyS opened this issue Aug 31, 2013 · 5 comments
Closed

Comments

@tobyS
Copy link

tobyS commented Aug 31, 2013

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

@goetas
Copy link
Collaborator

goetas commented Nov 8, 2013

I have the same issue

@koemeet
Copy link

koemeet commented Apr 25, 2014

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.

@koemeet
Copy link

koemeet commented Apr 25, 2014

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);
    }
}

@goetas
Copy link
Collaborator

goetas commented Apr 28, 2014

@steffenbrem Your implementation will not serialize the $packageName property (see https://gist.github.com/tobyS/6397023#file-gistfile1-php-L8 )

@goetas
Copy link
Collaborator

goetas commented Mar 13, 2017

This has to be solved with a custom handler (since the usecase explained here is a custom extension of the array object class)

@goetas goetas closed this as completed Mar 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants