Skip to content

[Serializer > JSON Encoding] Allow configuring default options #47183

@dbu

Description

@dbu

Description

I need to configure json encoding options (in my case JSON_PRESERVE_ZERO_FRACTION). but the configuration for the serializer section does not allow to set options.

It would be nice to allow to configure options for the encoder and decoder in the configuration.

Example

it would be nice if i could do something like this:

serializer:
    encoder: 
        options: JSON_PRESERVE_ZERO_FRACTION
    decoder: 
        options: JSON_THROW_ON_ERROR

my workaround is this compiler pass:

/**
 * Symfony serializer does not offer to configure options.
 */
class FloatSerializerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container): void
    {
        $serializer = $container->getDefinition('serializer.encoder.json');
        $encode = new Definition(JsonEncode::class);
        $encode->setArgument(0, [JsonEncode::OPTIONS => \JSON_PRESERVE_ZERO_FRACTION]);
        $container->setDefinition('zero-encoder', $encode);
        $serializer->setArgument('$encodingImpl', new Reference('zero-encoder'));
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy