Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

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
dbu opened this issue Aug 4, 2022 · 7 comments
Closed

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

dbu opened this issue Aug 4, 2022 · 7 comments

Comments

@dbu
Copy link
Contributor

dbu commented Aug 4, 2022

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'));
    }
}
@schodemeiss
Copy link

The serializer should allow you to set the context for the JSON Encoder via the Conext Builder.

https://github.com/symfony/symfony/blob/6.1/src/Symfony/Component/Serializer/Context/Encoder/JsonEncoderContextBuilder.php

Which should also means its customizable via the framework -> serializer -> default_context configuration, but alas, there is a bug which means that currently isn't working.

See for details: #47012

@nicolas-grekas
Copy link
Member

We should also set JSON_PRESERVE_ZERO_FRACTION by default!

@schodemeiss
Copy link

schodemeiss commented Aug 5, 2022

We should also set JSON_PRESERVE_ZERO_FRACTION by default!

Indeed - there's certainly a little work to be done around the serializer for 6.2. Unfortunately, I don't have the bandwidth to take a look at any of it :-(.

@dbu
Copy link
Contributor Author

dbu commented Aug 5, 2022

@nicolas-grekas i created the PR to set JSON_PRESERVE_ZERO_FRACTION by default. if #47012 is correct that it should be possible to configure the default context in that way, then this issue is actually a documentation request because it would already be possible to do what i wanted.

https://symfony.com/doc/current/reference/configuration/framework.html#serializer does not mention the default_context option of the serializer configuration.

@mpiot
Copy link
Contributor

mpiot commented Aug 5, 2022

For the JSON Encoder/Decoder, the context from the configuration is not used, as described in #47012 (comment)

@schodemeiss
Copy link

https://symfony.com/doc/current/reference/configuration/framework.html#serializer does not mention the default_context option of the serializer configuration.

Is is mentioned here, however: https://symfony.com/doc/current/serializer.html#serializer-context

fabpot added a commit that referenced this issue Aug 7, 2022
…ault (dbu)

This PR was merged into the 6.2 branch.

Discussion
----------

[Serializer] enable JSON_PRESERVE_ZERO_FRACTION by default

this makes json output float values that happen to be full numbers with a `.0`, so `4.0` or `0.0` instead of `4` resp `0`. that in turn helps when consuming the json in a type-safe language.

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | As discussed in #47183
| License       | MIT
| Doc PR        | -

In #47183, @nicolas-grekas said that we should enable the preserve zero fraction flag by default in the serializer.

I am a bit unsure if we should consider this a BC break, as it does change the output of JSON data. Should it be considered a bugfix against 6.1, a feature against 6.2 or a BC break that has to wait until 7.0?

Commits
-------

9480e2b enable JSON_PRESERVE_ZERO_FRACTION by default
@dbu
Copy link
Contributor Author

dbu commented Sep 29, 2022

the functionality to set configuration in the context has been implemented in #45554

i am adding documentation in symfony/symfony-docs#17305

@dbu dbu closed this as completed Sep 29, 2022
OskarStark added a commit to symfony/symfony-docs that referenced this issue Sep 30, 2022
This PR was merged into the 5.4 branch.

Discussion
----------

document serializer default_context

fix symfony/symfony#47183

add missing documentation for this serializer configuration option

Commits
-------

664bc4b document serializer default_context
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

5 participants