Skip to content

Commit

Permalink
[Serializer] forward the context from the JsonEncoder to JsonEncode a…
Browse files Browse the repository at this point in the history
…nd JsonDecode
  • Loading branch information
dbu authored and nicolas-grekas committed Sep 29, 2022
1 parent 07861aa commit f8c32e9
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Encoder/JsonEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
protected $encodingImpl;
protected $decodingImpl;

private $defaultContext = [
JsonDecode::ASSOCIATIVE => true,
];

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

/**
* {@inheritdoc}
*/
public function encode($data, string $format, array $context = [])
{
$context = array_merge($this->defaultContext, $context);

return $this->encodingImpl->encode($data, self::FORMAT, $context);
}

Expand All @@ -42,6 +49,8 @@ public function encode($data, string $format, array $context = [])
*/
public function decode(string $data, string $format, array $context = [])
{
$context = array_merge($this->defaultContext, $context);

return $this->decodingImpl->decode($data, self::FORMAT, $context);
}

Expand Down

0 comments on commit f8c32e9

Please sign in to comment.