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 c693778

Browse filesBrowse files
dbunicolas-grekas
authored andcommitted
[Serializer] forward the context from the JsonEncoder to JsonEncode and JsonDecode
1 parent fcc2174 commit c693778
Copy full SHA for c693778

File tree

1 file changed

+11
-2
lines changed
Filter options

1 file changed

+11
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
+11-2Lines 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($data, string $format, array $context = [])
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($data, string $format, array $context = [])
4249
*/
4350
public function decode(string $data, string $format, array $context = [])
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.