Skip to content

Commit

Permalink
bug #45554 [Serializer] Fixed framework.serializer.default_context is…
Browse files Browse the repository at this point in the history
… not working for JsonEncoder (siganushka)

This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] Fixed framework.serializer.default_context is not working for JsonEncoder

Fixed `framework.serializer.default_context` is not working for `JsonEncoder`

like `XmlEncoder` or `DateTimeNormalizer`, the `array $defaultContext` argument bindings from `SerializerPass`, but not working for `JsonEncoder`, I added `array $defaultContext` argument and passed to `JsonEncode` and `JsonDecode`.

https://github.com/symfony/serializer/blob/5.4/DependencyInjection/SerializerPass.php#L69-L75

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no

Commits
-------

a76016a [Serializer] Fixed framework.serializer.default_context is not working for JsonEncoder
  • Loading branch information
nicolas-grekas committed Sep 28, 2022
2 parents 3c3a493 + a76016a commit caad8ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
protected $encodingImpl;
protected $decodingImpl;

public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null)
public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null, array $defaultContext = [])
{
$this->encodingImpl = $encodingImpl ?? new JsonEncode();
$this->decodingImpl = $decodingImpl ?? new JsonDecode([JsonDecode::ASSOCIATIVE => true]);
$this->encodingImpl = $encodingImpl ?? new JsonEncode($defaultContext);
$this->decodingImpl = $decodingImpl ?? new JsonDecode(array_merge([JsonDecode::ASSOCIATIVE => true], $defaultContext));
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/JsonEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ public function testOptions()
$this->assertEquals($expected, $this->serializer->serialize($arr, 'json'), 'Context should not be persistent');
}

public function testWithDefaultContext()
{
$defaultContext = [
'json_encode_options' => \JSON_UNESCAPED_UNICODE,
'json_decode_associative' => false,
];

$encoder = new JsonEncoder(null, null, $defaultContext);

$data = new \stdClass();
$data->msg = '你好';

$this->assertEquals('{"msg":"你好"}', $json = $encoder->encode($data, 'json'));
$this->assertEquals($data, $encoder->decode($json, 'json'));
}

public function testEncodeNotUtf8WithoutPartialOnError()
{
$this->expectException(UnexpectedValueException::class);
Expand Down

0 comments on commit caad8ec

Please sign in to comment.