Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
Original file line number Diff line number Diff line change
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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
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