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

Commit caad8ec

Browse filesBrowse files
bug #45554 [Serializer] Fixed framework.serializer.default_context is 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
2 parents 3c3a493 + a76016a commit caad8ec
Copy full SHA for caad8ec

File tree

2 files changed

+19
-3
lines changed
Filter options

2 files changed

+19
-3
lines changed

‎src/Symfony/Component/Serializer/Encoder/JsonEncoder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
2323
protected $encodingImpl;
2424
protected $decodingImpl;
2525

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

3232
/**

‎src/Symfony/Component/Serializer/Tests/Encoder/JsonEncoderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Encoder/JsonEncoderTest.php
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ public function testOptions()
6666
$this->assertEquals($expected, $this->serializer->serialize($arr, 'json'), 'Context should not be persistent');
6767
}
6868

69+
public function testWithDefaultContext()
70+
{
71+
$defaultContext = [
72+
'json_encode_options' => \JSON_UNESCAPED_UNICODE,
73+
'json_decode_associative' => false,
74+
];
75+
76+
$encoder = new JsonEncoder(null, null, $defaultContext);
77+
78+
$data = new \stdClass();
79+
$data->msg = '你好';
80+
81+
$this->assertEquals('{"msg":"你好"}', $json = $encoder->encode($data, 'json'));
82+
$this->assertEquals($data, $encoder->decode($json, 'json'));
83+
}
84+
6985
public function testEncodeNotUtf8WithoutPartialOnError()
7086
{
7187
$this->expectException(UnexpectedValueException::class);

0 commit comments

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