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

Allow to set default serialization context when building serializer #528

Closed
nuxwin opened this issue Nov 22, 2015 · 3 comments
Closed

Allow to set default serialization context when building serializer #528

nuxwin opened this issue Nov 22, 2015 · 3 comments
Milestone

Comments

@nuxwin
Copy link

nuxwin commented Nov 22, 2015

Hello ;

Does it is possible to set default context when building the serializer?

Intent: For all our models, we want force serialization of null properties. Right now, we must pass a context object in each of our controllers. For instance:

...
$package = $this->getPackageService()->getPackage($this->getRequest()->query->getInt('package_id'));

$instance = new ApsInstance();
$instance
        ->setPackage($package)
        ->setSettings($this->getInstanceSettingService()->getSettingObjectsFromMetadataFile($package));

$context = new SerializationContext();
$context->setSerializeNull(true);

$json =  $this->getSerializer()->serialize($instance, 'json', $context);
...

Since we want such context for every serialization process, we would be able to do something like this in our Serializer service factory:

namespace iMSCP\Service;

use iMSCP_Registry as Registry;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializationContext;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
 * Class SerializerServiceFactory
 * @package iMSCP\Service
 */
class SerializerServiceFactory implements FactoryInterface
{
    /**
     * {@inheritdoc}
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $serviceLocator->get('Annotation');
        $config = Registry::get('config');

        $defaultContext = new SerializationContext();
        $defaultContext->setSerializeNull(true);

        return SerializerBuilder::create()
            ->setDefaultSerializationContext($defaultContext)
            ->setCacheDir(CACHE_PATH . '/serializer')
            ->setDebug($config['DEVMODE'])
            ->build();
    }
}
@alcalyn
Copy link
Contributor

alcalyn commented Dec 23, 2015

I had the same problem, then I created a PR: #541

@nuxwin
Copy link
Author

nuxwin commented Dec 23, 2015

@alcalyn

Please, don't enforce closure for the factory. Eg, in zf2 service manager, it is recommended to use factory classes which is much better. You could maybe enforce callable. This would allow invokables.

@goetas goetas added this to the v1.4 milestone Oct 23, 2016
@goetas
Copy link
Collaborator

goetas commented Oct 28, 2016

will be solved by #541 in v1.4

@goetas goetas closed this as completed Oct 28, 2016
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