Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. JsonEncoder.php

class JsonEncoder

Same name in this branch
  1. 11.1.x core/modules/jsonapi/src/Encoder/JsonEncoder.php \Drupal\jsonapi\Encoder\JsonEncoder
  2. 11.1.x core/modules/serialization/src/Encoder/JsonEncoder.php \Drupal\serialization\Encoder\JsonEncoder

Encodes JSON data.

@author Jordi Boggiano <j.boggiano@seld.be>

Hierarchy

  • class \Symfony\Component\Serializer\Encoder\JsonEncoder implements \Symfony\Component\Serializer\Encoder\EncoderInterface, \Symfony\Component\Serializer\Encoder\DecoderInterface

Expanded class hierarchy of JsonEncoder

3 files declare their use of JsonEncoder
AbstractObjectNormalizer.php in vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php
JsonEncoder.php in core/modules/serialization/src/Encoder/JsonEncoder.php
UserAuthenticationController.php in core/modules/user/src/Controller/UserAuthenticationController.php

File

vendor/symfony/serializer/Encoder/JsonEncoder.php, line 19

Namespace

Symfony\Component\Serializer\Encoder
View source
class JsonEncoder implements EncoderInterface, DecoderInterface {
    public const FORMAT = 'json';
    protected JsonEncode $encodingImpl;
    protected JsonDecode $decodingImpl;
    private array $defaultContext = [
        JsonDecode::ASSOCIATIVE => true,
    ];
    public function __construct(?JsonEncode $encodingImpl = null, ?JsonDecode $decodingImpl = null, array $defaultContext = []) {
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
        $this->encodingImpl = $encodingImpl ?? new JsonEncode($this->defaultContext);
        $this->decodingImpl = $decodingImpl ?? new JsonDecode($this->defaultContext);
    }
    public function encode(mixed $data, string $format, array $context = []) : string {
        $context = array_merge($this->defaultContext, $context);
        return $this->encodingImpl
            ->encode($data, self::FORMAT, $context);
    }
    public function decode(string $data, string $format, array $context = []) : mixed {
        $context = array_merge($this->defaultContext, $context);
        return $this->decodingImpl
            ->decode($data, self::FORMAT, $context);
    }
    public function supportsEncoding(string $format) : bool {
        return self::FORMAT === $format;
    }
    public function supportsDecoding(string $format) : bool {
        return self::FORMAT === $format;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
JsonEncoder::$decodingImpl protected property
JsonEncoder::$defaultContext private property
JsonEncoder::$encodingImpl protected property
JsonEncoder::decode public function Decodes a string into PHP data. Overrides DecoderInterface::decode
JsonEncoder::encode public function Encodes data into the given format. Overrides EncoderInterface::encode
JsonEncoder::FORMAT public constant
JsonEncoder::supportsDecoding public function Checks whether the deserializer can decode from given format. Overrides DecoderInterface::supportsDecoding 1
JsonEncoder::supportsEncoding public function Checks whether the serializer can encode to given format. Overrides EncoderInterface::supportsEncoding 1
JsonEncoder::__construct public function 1

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal