Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
Closed
@dbu

Description

@dbu
Issue body actions

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.