3 namespace Drupal\serialization\Encoder;
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;
12 * Adds 'ajax to the supported content types of the JSON encoder'
14 class JsonEncoder extends BaseJsonEncoder implements EncoderInterface, DecoderInterface {
17 * The formats that this Encoder supports.
21 protected static $format = ['json', 'ajax'];
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);
38 public function supportsEncoding($format) {
39 return in_array($format, static::$format);
45 public function supportsDecoding($format) {
46 return in_array($format, static::$format);