JsonEncoder.php in Drupal 8
File
core/modules/serialization/src/Encoder/JsonEncoder.php
View source
<?php
namespace Drupal\serialization\Encoder;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\Encoder\JsonEncoder as BaseJsonEncoder;
class JsonEncoder extends BaseJsonEncoder implements EncoderInterface, DecoderInterface {
protected static $format = [
'json',
'ajax',
];
public function __construct(JsonEncode $encodingImpl = NULL, JsonDecode $decodingImpl = NULL) {
$this->encodingImpl = $encodingImpl ?: $this
->getJsonEncode();
$this->decodingImpl = $decodingImpl ?: $this
->getJsonDecode();
}
private function getJsonEncode() {
$json_encoding_options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
$reflection = new \ReflectionClass(JsonEncode::class);
if (array_key_exists('OPTIONS', $reflection
->getConstants())) {
return new JsonEncode([
JsonEncode::OPTIONS => $json_encoding_options,
]);
}
return new JsonEncode($json_encoding_options);
}
private function getJsonDecode() {
$reflection = new \ReflectionClass(JsonDecode::class);
if (array_key_exists('ASSOCIATIVE', $reflection
->getConstants())) {
return new JsonDecode([
JsonDecode::ASSOCIATIVE => TRUE,
]);
}
return new JsonDecode(TRUE);
}
public function supportsEncoding($format) {
return in_array($format, static::$format);
}
public function supportsDecoding($format) {
return in_array($format, static::$format);
}
}
Classes
Name |
Description |
JsonEncoder |
Adds 'ajax to the supported content types of the JSON encoder' |