class JsonEncoder

Same name in this branch
  1. 8.9.x core/modules/jsonapi/src/Encoder/JsonEncoder.php \Drupal\jsonapi\Encoder\JsonEncoder
  2. 8.9.x core/modules/hal/src/Encoder/JsonEncoder.php \Drupal\hal\Encoder\JsonEncoder
Same name in other branches
  1. 9 core/modules/jsonapi/src/Encoder/JsonEncoder.php \Drupal\jsonapi\Encoder\JsonEncoder
  2. 9 core/modules/serialization/src/Encoder/JsonEncoder.php \Drupal\serialization\Encoder\JsonEncoder
  3. 9 core/modules/hal/src/Encoder/JsonEncoder.php \Drupal\hal\Encoder\JsonEncoder
  4. 10 core/modules/jsonapi/src/Encoder/JsonEncoder.php \Drupal\jsonapi\Encoder\JsonEncoder
  5. 10 core/modules/serialization/src/Encoder/JsonEncoder.php \Drupal\serialization\Encoder\JsonEncoder
  6. 11.x core/modules/jsonapi/src/Encoder/JsonEncoder.php \Drupal\jsonapi\Encoder\JsonEncoder
  7. 11.x core/modules/serialization/src/Encoder/JsonEncoder.php \Drupal\serialization\Encoder\JsonEncoder

Adds 'ajax to the supported content types of the JSON encoder'

@internal This encoder should not be used directly. Rather, use the `serializer` service.

Hierarchy

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

Expanded class hierarchy of JsonEncoder

5 files declare their use of JsonEncoder
DefaultExceptionSubscriberTest.php in core/modules/serialization/tests/src/Unit/EventSubscriber/DefaultExceptionSubscriberTest.php
JsonEncoder.php in core/modules/jsonapi/src/Encoder/JsonEncoder.php
JsonEncoder.php in core/modules/hal/src/Encoder/JsonEncoder.php
JsonEncoderTest.php in core/modules/serialization/tests/src/Unit/Encoder/JsonEncoderTest.php
ResourceResponseSubscriberTest.php in core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php
2 string references to 'JsonEncoder'
rest_test.services.yml in core/modules/rest/tests/modules/rest_test/rest_test.services.yml
core/modules/rest/tests/modules/rest_test/rest_test.services.yml
serialization.services.yml in core/modules/serialization/serialization.services.yml
core/modules/serialization/serialization.services.yml
2 services use JsonEncoder
rest_test.encoder.foobar in core/modules/rest/tests/modules/rest_test/rest_test.services.yml
Drupal\serialization\Encoder\JsonEncoder
serializer.encoder.json in core/modules/serialization/serialization.services.yml
Drupal\serialization\Encoder\JsonEncoder

File

core/modules/serialization/src/Encoder/JsonEncoder.php, line 18

Namespace

Drupal\serialization\Encoder
View source
class JsonEncoder extends BaseJsonEncoder implements EncoderInterface, DecoderInterface {
    
    /**
     * The formats that this Encoder supports.
     *
     * @var array
     */
    protected static $format = [
        'json',
        'ajax',
    ];
    
    /**
     * {@inheritdoc}
     */
    public function __construct(JsonEncode $encodingImpl = NULL, JsonDecode $decodingImpl = NULL) {
        $this->encodingImpl = $encodingImpl ?: $this->getJsonEncode();
        $this->decodingImpl = $decodingImpl ?: $this->getJsonDecode();
    }
    
    /**
     * Instantiates a JsonEncode instance.
     *
     * @internal this exists to bridge Symfony 3 to Symfony 4, and can be removed
     *   once Drupal requires Symfony 4.2 or higher.
     */
    private function getJsonEncode() {
        // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be
        // embedded into HTML.
        // @see \Symfony\Component\HttpFoundation\JsonResponse
        $json_encoding_options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
        $reflection = new \ReflectionClass(JsonEncode::class);
        if (array_key_exists('OPTIONS', $reflection->getConstants())) {
            return new JsonEncode([
                JsonEncode::OPTIONS => $json_encoding_options,
            ]);
        }
        return new JsonEncode($json_encoding_options);
    }
    
    /**
     * Instantiates a JsonDecode instance.
     *
     * @internal this exists to bridge Symfony 3 to Symfony 4, and can be removed
     *   once Drupal requires Symfony 4.2 or higher.
     */
    private function getJsonDecode() {
        $reflection = new \ReflectionClass(JsonDecode::class);
        if (array_key_exists('ASSOCIATIVE', $reflection->getConstants())) {
            return new JsonDecode([
                JsonDecode::ASSOCIATIVE => TRUE,
            ]);
        }
        return new JsonDecode(TRUE);
    }
    
    /**
     * {@inheritdoc}
     */
    public function supportsEncoding($format) {
        return in_array($format, static::$format);
    }
    
    /**
     * {@inheritdoc}
     */
    public function supportsDecoding($format) {
        return in_array($format, static::$format);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
JsonEncoder::$format protected static property The formats that this Encoder supports. 2
JsonEncoder::getJsonDecode private function Instantiates a JsonDecode instance.
JsonEncoder::getJsonEncode private function Instantiates a JsonEncode instance.
JsonEncoder::supportsDecoding public function
JsonEncoder::supportsEncoding public function
JsonEncoder::__construct public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.