@@ -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