0

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:
        #...
  • 1
    If you are on version 1.1 of JMSSerializerBundle it seems like you can use the option ` jms_serializer: enable_short_alias: false Can you try this? – Jeroen Jul 13 '20 at 14:00
  • @Jeroen Awesome, that seems to have fixed it. Now I just hope, that the existing code base does not use the short alias :) Thank you, now I can go on with my project. Do you have any input regarding my second question? just for future reference. –  Jul 13 '20 at 14:10
  • 1
    I do, see my answer! – Jeroen Jul 13 '20 at 14:35

1 Answers1

0

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

Jeroen
  • 1,991
  • 2
  • 16
  • 32