Skip to content

[Serializer] Encoder options from the framework bundle #28771

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

Closed
soullivaneuh opened this issue Oct 8, 2018 · 14 comments
Closed

[Serializer] Encoder options from the framework bundle #28771

soullivaneuh opened this issue Oct 8, 2018 · 14 comments

Comments

@soullivaneuh
Copy link
Contributor

soullivaneuh commented Oct 8, 2018

Description

AFAIK there is no way to provide a YamlEncoder default context or JsonEncoder json decoder service (with custom options) from configuration.

It would great to be able to do it.

Example

framework:
    serializer:
        name_converter: serializer.name_converter.camel_case_to_snake_case
        encoders:
             yaml:
                  inline: 10

Possible Workaround

On the kernel, overwrite the definitions (example with the yaml encoder):

final class Kernel extends BaseKernel implements CompilerPassInterface
{
    use MicroKernelTrait;

// ...

    public function process(ContainerBuilder $container): void
    {
        $container->getDefinition('serializer.encoder.yaml')
            ->setArguments([null, null, [
                'yaml_inline' => 10,
            ]])
        ;

// ...
@soullivaneuh
Copy link
Contributor Author

Also, a default context should be provided on the JsonDecoder constructor.

@soullivaneuh
Copy link
Contributor Author

soullivaneuh commented Oct 8, 2018

Little bit trickier workaround example with JsonDecoder, using service decoration:

namespace App\Serializer\Encoder;

use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;

/**
 * @see https://github.com/symfony/symfony/issues/28771
 */
final class JsonEncoder implements EncoderInterface, DecoderInterface
{
    /**
     * @var \Symfony\Component\Serializer\Encoder\JsonEncoder
     */
    private $encoder;

    /**
     * @var bool
     */
    private $kernelDebug;

    public function __construct(\Symfony\Component\Serializer\Encoder\JsonEncoder $encoder, bool $kernelDebug)
    {
        $this->encoder = $encoder;
        $this->kernelDebug = $kernelDebug;
    }

    /**
     * {@inheritdoc}
     */
    public function encode($data, $format, array $context = array())
    {
        $context['json_encode_options'] = JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION | ($context['json_encode_options'] ?? 0);

        if ($this->kernelDebug) {
            $context['json_encode_options'] |= JSON_PRETTY_PRINT;
        }

        return $this->encoder->encode($data, $format, $context);
    }

    /**
     * {@inheritdoc}
     */
    public function supportsEncoding($format)
    {
        return $this->encoder->supportsEncoding($format);
    }

    /**
     * {@inheritdoc}
     */
    public function decode($data, $format, array $context = array())
    {
        return $this->encoder->decode($data, $format, $context);
    }

    /**
     * {@inheritdoc}
     */
    public function supportsDecoding($format)
    {
        return $this->encoder->supportsDecoding($format);
    }
}
services:
    App\Serializer\Encoder\JsonEncoder:
        decorates: serializer.encoder.json

@ogizanagi
Copy link
Contributor

Next steps described in #28709 may allow this by configuring defaultContext through framework.serializer config.

@Simperfit
Copy link
Contributor

what can we do about this one so ? is there anything to code ? @ogizanagi

@ogizanagi
Copy link
Contributor

@Simperfit : Already a work in progress in #28927 :)

@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@carsonbot
Copy link

Could I get a reply or should I close this?

@carsonbot
Copy link

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

@maxhelias
Copy link
Contributor

Work in progress in new PR #38542

@xabbuh xabbuh reopened this Jan 17, 2021
@Nyholm Nyholm removed the Stalled label Feb 22, 2021
@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@carsonbot
Copy link

Just a quick reminder to make a comment on this. If I don't hear anything I'll close this.

@carsonbot
Copy link

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

@stof stof reopened this Sep 20, 2021
@stof
Copy link
Member

stof commented Sep 20, 2021

Reopening as we have a WIP PR.

@carsonbot carsonbot removed the Stalled label Sep 20, 2021
@bigfoot90
Copy link

bigfoot90 commented Jan 6, 2022

Work in progress in new PR #38542

#38542 Merged. Can this be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants