Skip to content

Commit 699d42e

Browse files
Merge branch '6.1' into 6.2
* 6.1: [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 c5b4aa4 + 0986800 commit 699d42e

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,19 +23,28 @@ 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
public function encode(mixed $data, string $format, array $context = []): string
3338
{
39+
$context = array_merge($this->defaultContext, $context);
40+
3441
return $this->encodingImpl->encode($data, self::FORMAT, $context);
3542
}
3643

3744
public function decode(string $data, string $format, array $context = []): mixed
3845
{
46+
$context = array_merge($this->defaultContext, $context);
47+
3948
return $this->decodingImpl->decode($data, self::FORMAT, $context);
4049
}
4150

0 commit comments

Comments
 (0)