Search code examples
symfonyjmsserializerbundlejms-serializer

symfony serializer jmsserializerbundle service name conflict


I have the following issue: I am working on a symfony (2.8) project which depends on the jmsserializerbundle (1.1). When enabling the symfony-serializer alongside the jms-serializer package,

# app/config/config.yml
framework:
    # ...
    serializer: { enabled: true }

jms_serializer:
    metadata:
        #...

upon calling $this->get('serializer') or $this->get('jms_serializer') I only get the jms-serializer. This issue seems to have been resolved in jmsserializerbundle version 2.0: https://github.com/schmittjoh/JMSSerializerBundle/issues/558

Is there any way to solve this without updating jmsserializerbundle to 2.0?

Would there be any difference in performance compared to the normal symfony-serializer configuration, when wrapping a symfony-serializer in a custom service? like so:

<?php

use SomeCustomNormalizer;
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

class SerializerService implements SerializerInterface
{

    private $serializer;

    public function __construct()
    {
        $this->serializer = new Serializer(
            [new SomeCustomNormalizer(), new ObjectNormalizer()],
            [new JsonEncode()]
        );
    }

    public function serialize($data, $format, array $context = array())
    {
        # serialize
    }

    public function deserialize($data, $type, $format, array $context = array())
    {
        # deserialize
    }
}
# SomeBundle/Resources/config/services.yml
serializer_service:
      class: SomeBundle\SerializerService

The question regarding the performance came up for me because the existing jms configuration registers the jmsserializerbundle in the app kernel, which is not the case my custom service, which is just set up in services.yml.

Thanks in advance

Solution

As described below I just had to add one line to the jms-config:

# app/config/config.yml
jms_serializer:
    enable_short_alias: false
    metadata:
        #...

Solution

  • Is there any way to solve this without updating jmsserializerbundle to 2.0?

    JMS Serializer provides the option:

    jms_serializer:
        enable_short_alias: false
    

    Would there be any difference in performance compared to the normal symfony-serializer configuration when wrapping a Symfony-serializer in a custom service? like so:

    I guess not, the Symfony serializer is just 'another' service defined by the FrameworkBundle, a wrapper around the Serializer class with the normalizers and encoders injected.

    If you create your own service (like in your example) it will be compiled by the service container as well. You can check the definition here: https://github.com/symfony/symfony/blob/v2.8.52/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml