-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Comments
Also, a default context should be provided on the |
Little bit trickier workaround example with 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 |
Next steps described in #28709 may allow this by configuring defaultContext through |
what can we do about this one so ? is there anything to code ? @ogizanagi |
@Simperfit : Already a work in progress in #28927 :) |
Thank you for this suggestion. |
Could I get a reply or should I close this? |
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! |
Work in progress in new PR #38542 |
Thank you for this suggestion. |
Just a quick reminder to make a comment on this. If I don't hear anything I'll close this. |
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! |
Reopening as we have a WIP PR. |
Description
AFAIK there is no way to provide a
YamlEncoder
default context orJsonEncoder
json decoder service (with custom options) from configuration.It would great to be able to do it.
Example
Possible Workaround
On the kernel, overwrite the definitions (example with the yaml encoder):
The text was updated successfully, but these errors were encountered: