You are here

public function JsonEncoder::encode in JSON:API 8

See also

http://jsonapi.org/format/#errors

File

src/Encoder/JsonEncoder.php, line 27

Class

JsonEncoder
Encodes JSON API data.

Namespace

Drupal\jsonapi\Encoder

Code

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);
}