You are here

class JsonEncoder in JSON:API 8

Same name and namespace in other branches
  1. 8.2 src/Encoder/JsonEncoder.php \Drupal\jsonapi\Encoder\JsonEncoder

Encodes JSON API data.

@internal

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

1 string reference to 'JsonEncoder'
jsonapi.services.yml in ./jsonapi.services.yml
jsonapi.services.yml
1 service uses JsonEncoder
serializer.encoder.jsonapi in ./jsonapi.services.yml
Drupal\jsonapi\Encoder\JsonEncoder

File

src/Encoder/JsonEncoder.php, line 13

Namespace

Drupal\jsonapi\Encoder
View source
class JsonEncoder extends SerializationJsonEncoder {

  /**
   * The formats that this Encoder supports.
   *
   * @var string
   */
  protected static $format = [
    'api_json',
  ];

  /**
   * {@inheritdoc}
   *
   * @see http://jsonapi.org/format/#errors
   */
  public function encode($data, $format, array $context = []) {

    // Make sure that any auto-normalizable object gets normalized before
    // encoding. This is specially important to generate the errors in partial
    // success responses.
    if ($data instanceof ValueExtractorInterface) {
      $data = $data
        ->rasterizeValue();
    }

    // Allows wrapping the encoded output. This is so we can use the same
    // encoder and normalizers when serializing HttpExceptions to match the
    // JSON API specification.
    if (!empty($context['data_wrapper'])) {
      $data = [
        $context['data_wrapper'] => $data,
      ];
    }
    return parent::encode($data, $format, $context);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JsonEncoder::$format protected static property The formats that this Encoder supports. Overrides JsonEncoder::$format
JsonEncoder::encode public function
JsonEncoder::getJsonDecode private function Instantiates a JsonDecode instance.
JsonEncoder::getJsonEncode private function Instantiates a JsonEncode instance.
JsonEncoder::supportsDecoding public function Checks whether the deserializer can decode from given format.
JsonEncoder::supportsEncoding public function Checks whether the serializer can encode to given format.
JsonEncoder::__construct public function