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

Breadcrumb

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

class JsonEncode

Encodes JSON data.

@author Sander Coolen <sander@jibber.nl>

Hierarchy

  • class \Symfony\Component\Serializer\Encoder\JsonEncode implements \Symfony\Component\Serializer\Encoder\EncoderInterface

Expanded class hierarchy of JsonEncode

2 files declare their use of JsonEncode
JsonEncoder.php in core/modules/serialization/src/Encoder/JsonEncoder.php
JsonEncoderContextBuilder.php in vendor/symfony/serializer/Context/Encoder/JsonEncoderContextBuilder.php

File

vendor/symfony/serializer/Encoder/JsonEncode.php, line 21

Namespace

Symfony\Component\Serializer\Encoder
View source
class JsonEncode implements EncoderInterface {
    
    /**
     * Configure the JSON flags bitmask.
     */
    public const OPTIONS = 'json_encode_options';
    private array $defaultContext = [
        self::OPTIONS => \JSON_PRESERVE_ZERO_FRACTION,
    ];
    public function __construct(array $defaultContext = []) {
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
    }
    public function encode(mixed $data, string $format, array $context = []) : string {
        $options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];
        try {
            $encodedJson = json_encode($data, $options);
        } catch (\JsonException $e) {
            throw new NotEncodableValueException($e->getMessage(), 0, $e);
        }
        if (\JSON_THROW_ON_ERROR & $options) {
            return $encodedJson;
        }
        if (\JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($options & \JSON_PARTIAL_OUTPUT_ON_ERROR))) {
            throw new NotEncodableValueException(json_last_error_msg());
        }
        return $encodedJson;
    }
    public function supportsEncoding(string $format) : bool {
        return JsonEncoder::FORMAT === $format;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
JsonEncode::$defaultContext private property
JsonEncode::encode public function Encodes data into the given format. Overrides EncoderInterface::encode
JsonEncode::OPTIONS public constant Configure the JSON flags bitmask.
JsonEncode::supportsEncoding public function Checks whether the serializer can encode to given format. Overrides EncoderInterface::supportsEncoding
JsonEncode::__construct public function
RSS feed
Powered by Drupal