annotate core/modules/serialization/src/Encoder/JsonEncoder.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
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@14 13 *
Chris@14 14 * @internal
Chris@14 15 * This encoder should not be used directly. Rather, use the `serializer`
Chris@14 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@18 31 $this->encodingImpl = $encodingImpl ?: $this->getJsonEncode();
Chris@18 32 $this->decodingImpl = $decodingImpl ?: $this->getJsonDecode();
Chris@18 33 }
Chris@18 34
Chris@18 35 /**
Chris@18 36 * Instantiates a JsonEncode instance.
Chris@18 37 *
Chris@18 38 * @internal this exists to bridge Symfony 3 to Symfony 4, and can be removed
Chris@18 39 * once Drupal requires Symfony 4.2 or higher.
Chris@18 40 */
Chris@18 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@18 46 $reflection = new \ReflectionClass(JsonEncode::class);
Chris@18 47 if (array_key_exists('OPTIONS', $reflection->getConstants())) {
Chris@18 48 return new JsonEncode([JsonEncode::OPTIONS => $json_encoding_options]);
Chris@18 49 }
Chris@18 50 return new JsonEncode($json_encoding_options);
Chris@18 51 }
Chris@18 52
Chris@18 53 /**
Chris@18 54 * Instantiates a JsonDecode instance.
Chris@18 55 *
Chris@18 56 * @internal this exists to bridge Symfony 3 to Symfony 4, and can be removed
Chris@18 57 * once Drupal requires Symfony 4.2 or higher.
Chris@18 58 */
Chris@18 59 private function getJsonDecode() {
Chris@18 60 $reflection = new \ReflectionClass(JsonDecode::class);
Chris@18 61 if (array_key_exists('ASSOCIATIVE', $reflection->getConstants())) {
Chris@18 62 return new JsonDecode([JsonDecode::ASSOCIATIVE => TRUE]);
Chris@18 63 }
Chris@18 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 }