Skip to content

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

Closed
@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