Issue #3135304 by longwave: Remove Symfony 3 compatibility layer from JsonEncoder
parent
1ea2763329
commit
d02bccbf9d
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
namespace Drupal\serialization\Encoder;
|
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\JsonDecode;
|
||||||
use Symfony\Component\Serializer\Encoder\JsonEncode;
|
use Symfony\Component\Serializer\Encoder\JsonEncode;
|
||||||
use Symfony\Component\Serializer\Encoder\JsonEncoder as BaseJsonEncoder;
|
use Symfony\Component\Serializer\Encoder\JsonEncoder as BaseJsonEncoder;
|
||||||
|
@ -15,7 +13,7 @@ use Symfony\Component\Serializer\Encoder\JsonEncoder as BaseJsonEncoder;
|
||||||
* This encoder should not be used directly. Rather, use the `serializer`
|
* This encoder should not be used directly. Rather, use the `serializer`
|
||||||
* service.
|
* service.
|
||||||
*/
|
*/
|
||||||
class JsonEncoder extends BaseJsonEncoder implements EncoderInterface, DecoderInterface {
|
class JsonEncoder extends BaseJsonEncoder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The formats that this Encoder supports.
|
* The formats that this Encoder supports.
|
||||||
|
@ -28,40 +26,12 @@ class JsonEncoder extends BaseJsonEncoder implements EncoderInterface, DecoderIn
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function __construct(JsonEncode $encodingImpl = NULL, JsonDecode $decodingImpl = NULL) {
|
public function __construct(JsonEncode $encodingImpl = NULL, JsonDecode $decodingImpl = NULL) {
|
||||||
$this->encodingImpl = $encodingImpl ?: $this->getJsonEncode();
|
|
||||||
$this->decodingImpl = $decodingImpl ?: $this->getJsonDecode();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Instantiates a JsonEncode instance.
|
|
||||||
*
|
|
||||||
* @internal this exists to bridge Symfony 3 to Symfony 4, and can be removed
|
|
||||||
* once Drupal requires Symfony 4.2 or higher.
|
|
||||||
*/
|
|
||||||
private function getJsonEncode() {
|
|
||||||
// Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be
|
// Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be
|
||||||
// embedded into HTML.
|
// embedded into HTML.
|
||||||
// @see \Symfony\Component\HttpFoundation\JsonResponse
|
// @see \Symfony\Component\HttpFoundation\JsonResponse
|
||||||
$json_encoding_options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
|
$json_encoding_options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
|
||||||
$reflection = new \ReflectionClass(JsonEncode::class);
|
$this->encodingImpl = $encodingImpl ?: new JsonEncode([JsonEncode::OPTIONS => $json_encoding_options]);
|
||||||
if (array_key_exists('OPTIONS', $reflection->getConstants())) {
|
$this->decodingImpl = $decodingImpl ?: new JsonDecode([JsonDecode::ASSOCIATIVE => TRUE]);
|
||||||
return new JsonEncode([JsonEncode::OPTIONS => $json_encoding_options]);
|
|
||||||
}
|
|
||||||
return new JsonEncode($json_encoding_options);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Instantiates a JsonDecode instance.
|
|
||||||
*
|
|
||||||
* @internal this exists to bridge Symfony 3 to Symfony 4, and can be removed
|
|
||||||
* once Drupal requires Symfony 4.2 or higher.
|
|
||||||
*/
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue