You are here

class JsonEncoder in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/serializer/Encoder/JsonEncoder.php \Symfony\Component\Serializer\Encoder\JsonEncoder
  2. 8 core/modules/hal/src/Encoder/JsonEncoder.php \Drupal\hal\Encoder\JsonEncoder
  3. 8 core/modules/serialization/src/Encoder/JsonEncoder.php \Drupal\serialization\Encoder\JsonEncoder
Same name and namespace in other branches
  1. 8.0 vendor/symfony/serializer/Encoder/JsonEncoder.php \Symfony\Component\Serializer\Encoder\JsonEncoder

Encodes JSON data.

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

Hierarchy

Expanded class hierarchy of JsonEncoder

4 files declare their use of JsonEncoder
JsonEncoder.php in core/modules/hal/src/Encoder/JsonEncoder.php
Contains \Drupal\hal\Encoder\JsonEncoder.
JsonEncoder.php in core/modules/serialization/src/Encoder/JsonEncoder.php
Contains \Drupal\serialization\Encoder\JsonEncoder.
JsonEncoderTest.php in vendor/symfony/serializer/Tests/Encoder/JsonEncoderTest.php
SerializerTest.php in vendor/symfony/serializer/Tests/SerializerTest.php

File

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

Namespace

Symfony\Component\Serializer\Encoder
View source
class JsonEncoder implements EncoderInterface, DecoderInterface {
  const FORMAT = 'json';

  /**
   * @var JsonEncode
   */
  protected $encodingImpl;

  /**
   * @var JsonDecode
   */
  protected $decodingImpl;
  public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null) {
    $this->encodingImpl = $encodingImpl ?: new JsonEncode();
    $this->decodingImpl = $decodingImpl ?: new JsonDecode(true);
  }

  /**
   * Returns the last encoding error (if any).
   *
   * @return int
   *
   * @deprecated since version 2.5, to be removed in 3.0. JsonEncode throws exception if an error is found.
   */
  public function getLastEncodingError() {
    @trigger_error('The ' . __METHOD__ . ' method is deprecated since version 2.5 and will be removed in 3.0. Catch the exception raised by the Symfony\\Component\\Serializer\\Encoder\\JsonEncode::encode() method instead to get the last JSON encoding error.', E_USER_DEPRECATED);
    return $this->encodingImpl
      ->getLastError();
  }

  /**
   * Returns the last decoding error (if any).
   *
   * @return int
   *
   * @deprecated since version 2.5, to be removed in 3.0. JsonDecode throws exception if an error is found.
   */
  public function getLastDecodingError() {
    @trigger_error('The ' . __METHOD__ . ' method is deprecated since version 2.5 and will be removed in 3.0. Catch the exception raised by the Symfony\\Component\\Serializer\\Encoder\\JsonDecode::decode() method instead to get the last JSON decoding error.', E_USER_DEPRECATED);
    return $this->decodingImpl
      ->getLastError();
  }

  /**
   * {@inheritdoc}
   */
  public function encode($data, $format, array $context = array()) {
    return $this->encodingImpl
      ->encode($data, self::FORMAT, $context);
  }

  /**
   * {@inheritdoc}
   */
  public function decode($data, $format, array $context = array()) {
    return $this->decodingImpl
      ->decode($data, self::FORMAT, $context);
  }

  /**
   * {@inheritdoc}
   */
  public function supportsEncoding($format) {
    return self::FORMAT === $format;
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDecoding($format) {
    return self::FORMAT === $format;
  }

  /**
   * Resolves json_last_error message.
   *
   * @return string
   */
  public static function getLastErrorMessage() {
    if (function_exists('json_last_error_msg')) {
      return json_last_error_msg();
    }
    switch (json_last_error()) {
      case JSON_ERROR_DEPTH:
        return 'Maximum stack depth exceeded';
      case JSON_ERROR_STATE_MISMATCH:
        return 'Underflow or the modes mismatch';
      case JSON_ERROR_CTRL_CHAR:
        return 'Unexpected control character found';
      case JSON_ERROR_SYNTAX:
        return 'Syntax error, malformed JSON';
      case JSON_ERROR_UTF8:
        return 'Malformed UTF-8 characters, possibly incorrectly encoded';
      default:
        return 'Unknown error';
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JsonEncoder::$decodingImpl protected 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 constant
JsonEncoder::getLastDecodingError Deprecated public function Returns the last decoding error (if any).
JsonEncoder::getLastEncodingError Deprecated public function Returns the last encoding error (if any).
JsonEncoder::getLastErrorMessage public static function Resolves json_last_error message.
JsonEncoder::supportsDecoding public function Checks whether the deserializer can decode from given format. Overrides DecoderInterface::supportsDecoding 2
JsonEncoder::supportsEncoding public function Checks whether the serializer can encode to given format. Overrides EncoderInterface::supportsEncoding 2
JsonEncoder::__construct public function