Skip to content

Commit

Permalink
Merge branch '6.1' into 6.2
Browse files Browse the repository at this point in the history
* 6.1:
  [DI] fix tests
  [FrameworkBundle] fix merge
  [FrameworkBundle] Filter out trans paths that are covered by a parent folder path
  [Form] fix tests
  [Serializer] Fixed framework.serializer.default_context is not working for JsonEncoder
  [Ldap] Do not run ldap_set_option on failed connection
  Correct compare float data
  Bugfix: add \UnitEnum as a result of get() method
  Fix AbstractFormLoginAuthenticator return types (fixes #47571).
  Run composer with --ignore-platform-req=php+
  [FrameworkBundle] Fix a phpdoc in mailer assertions
  • Loading branch information
nicolas-grekas committed Sep 28, 2022
2 parents d2085ff + d90474a commit c5b4aa4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Encoder/JsonEncoder.php
Expand Up @@ -23,10 +23,10 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
protected $encodingImpl;
protected $decodingImpl;

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

public function encode(mixed $data, string $format, array $context = []): string
Expand Down
16 changes: 16 additions & 0 deletions Tests/Encoder/JsonEncoderTest.php
Expand Up @@ -66,6 +66,22 @@ public function testOptions()
$this->assertEquals($expected, $this->serializer->serialize($arr, 'json'), 'Context should not be persistent');
}

public function testWithDefaultContext()
{
$defaultContext = [
'json_encode_options' => \JSON_UNESCAPED_UNICODE,
'json_decode_associative' => false,
];

$encoder = new JsonEncoder(null, null, $defaultContext);

$data = new \stdClass();
$data->msg = '你好';

$this->assertEquals('{"msg":"你好"}', $json = $encoder->encode($data, 'json'));
$this->assertEquals($data, $encoder->decode($json, 'json'));
}

public function testEncodeNotUtf8WithoutPartialOnError()
{
$this->expectException(UnexpectedValueException::class);
Expand Down

0 comments on commit c5b4aa4

Please sign in to comment.