Version 1
[yaffs-website] / web / core / modules / serialization / src / Encoder / JsonEncoder.php
1 <?php
2
3 namespace Drupal\serialization\Encoder;
4
5 use Symfony\Component\Serializer\Encoder\DecoderInterface;
6 use Symfony\Component\Serializer\Encoder\EncoderInterface;
7 use Symfony\Component\Serializer\Encoder\JsonDecode;
8 use Symfony\Component\Serializer\Encoder\JsonEncode;
9 use Symfony\Component\Serializer\Encoder\JsonEncoder as BaseJsonEncoder;
10
11 /**
12  * Adds 'ajax to the supported content types of the JSON encoder'
13  */
14 class JsonEncoder extends BaseJsonEncoder implements EncoderInterface, DecoderInterface {
15
16   /**
17    * The formats that this Encoder supports.
18    *
19    * @var array
20    */
21   protected static $format = ['json', 'ajax'];
22
23   /**
24    * {@inheritdoc}
25    */
26   public function __construct(JsonEncode $encodingImpl = NULL, JsonDecode $decodingImpl = NULL) {
27     // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be
28     // embedded into HTML.
29     // @see \Symfony\Component\HttpFoundation\JsonResponse
30     $json_encoding_options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
31     $this->encodingImpl = $encodingImpl ?: new JsonEncode($json_encoding_options);
32     $this->decodingImpl = $decodingImpl ?: new JsonDecode(TRUE);
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function supportsEncoding($format) {
39     return in_array($format, static::$format);
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function supportsDecoding($format) {
46     return in_array($format, static::$format);
47   }
48
49 }