annotate core/modules/serialization/src/Encoder/JsonEncoder.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\serialization\Encoder;
Chris@0 4
Chris@0 5 use Symfony\Component\Serializer\Encoder\DecoderInterface;
Chris@0 6 use Symfony\Component\Serializer\Encoder\EncoderInterface;
Chris@0 7 use Symfony\Component\Serializer\Encoder\JsonDecode;
Chris@0 8 use Symfony\Component\Serializer\Encoder\JsonEncode;
Chris@0 9 use Symfony\Component\Serializer\Encoder\JsonEncoder as BaseJsonEncoder;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Adds 'ajax to the supported content types of the JSON encoder'
Chris@0 13 *
Chris@0 14 * @internal
Chris@0 15 * This encoder should not be used directly. Rather, use the `serializer`
Chris@0 16 * service.
Chris@0 17 */
Chris@0 18 class JsonEncoder extends BaseJsonEncoder implements EncoderInterface, DecoderInterface {
Chris@0 19
Chris@0 20 /**
Chris@0 21 * The formats that this Encoder supports.
Chris@0 22 *
Chris@0 23 * @var array
Chris@0 24 */
Chris@0 25 protected static $format = ['json', 'ajax'];
Chris@0 26
Chris@0 27 /**
Chris@0 28 * {@inheritdoc}
Chris@0 29 */
Chris@0 30 public function __construct(JsonEncode $encodingImpl = NULL, JsonDecode $decodingImpl = NULL) {
Chris@5 31 $this->encodingImpl = $encodingImpl ?: $this->getJsonEncode();
Chris@5 32 $this->decodingImpl = $decodingImpl ?: $this->getJsonDecode();
Chris@5 33 }
Chris@5 34
Chris@5 35 /**
Chris@5 36 * Instantiates a JsonEncode instance.
Chris@5 37 *
Chris@5 38 * @internal this exists to bridge Symfony 3 to Symfony 4, and can be removed
Chris@5 39 * once Drupal requires Symfony 4.2 or higher.
Chris@5 40 */
Chris@5 41 private function getJsonEncode() {
Chris@0 42 // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be
Chris@0 43 // embedded into HTML.
Chris@0 44 // @see \Symfony\Component\HttpFoundation\JsonResponse
Chris@0 45 $json_encoding_options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
Chris@5 46 $reflection = new \ReflectionClass(JsonEncode::class);
Chris@5 47 if (array_key_exists('OPTIONS', $reflection->getConstants())) {
Chris@5 48 return new JsonEncode([JsonEncode::OPTIONS => $json_encoding_options]);
Chris@5 49 }
Chris@5 50 return new JsonEncode($json_encoding_options);
Chris@5 51 }
Chris@5 52
Chris@5 53 /**
Chris@5 54 * Instantiates a JsonDecode instance.
Chris@5 55 *
Chris@5 56 * @internal this exists to bridge Symfony 3 to Symfony 4, and can be removed
Chris@5 57 * once Drupal requires Symfony 4.2 or higher.
Chris@5 58 */
Chris@5 59 private function getJsonDecode() {
Chris@5 60 $reflection = new \ReflectionClass(JsonDecode::class);
Chris@5 61 if (array_key_exists('ASSOCIATIVE', $reflection->getConstants())) {
Chris@5 62 return new JsonDecode([JsonDecode::ASSOCIATIVE => TRUE]);
Chris@5 63 }
Chris@5 64 return new JsonDecode(TRUE);
Chris@0 65 }
Chris@0 66
Chris@0 67 /**
Chris@0 68 * {@inheritdoc}
Chris@0 69 */
Chris@0 70 public function supportsEncoding($format) {
Chris@0 71 return in_array($format, static::$format);
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * {@inheritdoc}
Chris@0 76 */
Chris@0 77 public function supportsDecoding($format) {
Chris@0 78 return in_array($format, static::$format);
Chris@0 79 }
Chris@0 80
Chris@0 81 }