vendor/symfony/serializer/Encoder/JsonEncoder.php line 35

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Serializer\Encoder;
  11. /**
  12.  * Encodes JSON data.
  13.  *
  14.  * @author Jordi Boggiano <j.boggiano@seld.be>
  15.  */
  16. class JsonEncoder implements EncoderInterfaceDecoderInterface
  17. {
  18.     public const FORMAT 'json';
  19.     protected $encodingImpl;
  20.     protected $decodingImpl;
  21.     public function __construct(JsonEncode $encodingImpl nullJsonDecode $decodingImpl null)
  22.     {
  23.         $this->encodingImpl $encodingImpl ?? new JsonEncode();
  24.         $this->decodingImpl $decodingImpl ?? new JsonDecode([JsonDecode::ASSOCIATIVE => true]);
  25.     }
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     public function encode(mixed $datastring $format, array $context = []): string
  30.     {
  31.         return $this->encodingImpl->encode($dataself::FORMAT$context);
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function decode(string $datastring $format, array $context = []): mixed
  37.     {
  38.         return $this->decodingImpl->decode($dataself::FORMAT$context);
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      *
  43.      * @param array $context
  44.      */
  45.     public function supportsEncoding(string $format /*, array $context = [] */): bool
  46.     {
  47.         return self::FORMAT === $format;
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      *
  52.      * @param array $context
  53.      */
  54.     public function supportsDecoding(string $format /*, array $context = [] */): bool
  55.     {
  56.         return self::FORMAT === $format;
  57.     }
  58. }