Skip to content

Commit 0986800

Browse files
Merge branch '6.0' into 6.1
* 6.0: [Serializer] forward the context from the JsonEncoder to JsonEncode and JsonDecode [Notifier] Use local copy of stella-maris/clock when testing [FrameworkBundle] Fix passing `serializer.default_context` option to normalizers
2 parents d90474a + 296f619 commit 0986800

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Encoder/JsonEncoder.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,24 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
2323
protected $encodingImpl;
2424
protected $decodingImpl;
2525

26+
private $defaultContext = [
27+
JsonDecode::ASSOCIATIVE => true,
28+
];
29+
2630
public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null, array $defaultContext = [])
2731
{
28-
$this->encodingImpl = $encodingImpl ?? new JsonEncode($defaultContext);
29-
$this->decodingImpl = $decodingImpl ?? new JsonDecode(array_merge([JsonDecode::ASSOCIATIVE => true], $defaultContext));
32+
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
33+
$this->encodingImpl = $encodingImpl ?? new JsonEncode($this->defaultContext);
34+
$this->decodingImpl = $decodingImpl ?? new JsonDecode($this->defaultContext);
3035
}
3136

3237
/**
3338
* {@inheritdoc}
3439
*/
3540
public function encode(mixed $data, string $format, array $context = []): string
3641
{
42+
$context = array_merge($this->defaultContext, $context);
43+
3744
return $this->encodingImpl->encode($data, self::FORMAT, $context);
3845
}
3946

@@ -42,6 +49,8 @@ public function encode(mixed $data, string $format, array $context = []): string
4249
*/
4350
public function decode(string $data, string $format, array $context = []): mixed
4451
{
52+
$context = array_merge($this->defaultContext, $context);
53+
4554
return $this->decodingImpl->decode($data, self::FORMAT, $context);
4655
}
4756

0 commit comments

Comments
 (0)