annotate core/modules/serialization/src/Encoder/JsonEncoder.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children af1871eacc83
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@0 31 // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be
Chris@0 32 // embedded into HTML.
Chris@0 33 // @see \Symfony\Component\HttpFoundation\JsonResponse
Chris@0 34 $json_encoding_options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
Chris@0 35 $this->encodingImpl = $encodingImpl ?: new JsonEncode($json_encoding_options);
Chris@0 36 $this->decodingImpl = $decodingImpl ?: new JsonDecode(TRUE);
Chris@0 37 }
Chris@0 38
Chris@0 39 /**
Chris@0 40 * {@inheritdoc}
Chris@0 41 */
Chris@0 42 public function supportsEncoding($format) {
Chris@0 43 return in_array($format, static::$format);
Chris@0 44 }
Chris@0 45
Chris@0 46 /**
Chris@0 47 * {@inheritdoc}
Chris@0 48 */
Chris@0 49 public function supportsDecoding($format) {
Chris@0 50 return in_array($format, static::$format);
Chris@0 51 }
Chris@0 52
Chris@0 53 }