class JsonEncoder in JSON:API 8
Same name and namespace in other branches
- 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
- class \Drupal\jsonapi\Encoder\JsonEncoder
Expanded class hierarchy of JsonEncoder
1 string reference to 'JsonEncoder'
1 service uses JsonEncoder
File
- src/
Encoder/ JsonEncoder.php, line 13
Namespace
Drupal\jsonapi\EncoderView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
JsonEncoder:: |
protected static | property |
The formats that this Encoder supports. Overrides JsonEncoder:: |
|
JsonEncoder:: |
public | function | ||
JsonEncoder:: |
private | function | Instantiates a JsonDecode instance. | |
JsonEncoder:: |
private | function | Instantiates a JsonEncode instance. | |
JsonEncoder:: |
public | function | Checks whether the deserializer can decode from given format. | |
JsonEncoder:: |
public | function | Checks whether the serializer can encode to given format. | |
JsonEncoder:: |
public | function |