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'
15 * This encoder should not be used directly. Rather, use the `serializer`
18 class JsonEncoder extends BaseJsonEncoder implements EncoderInterface, DecoderInterface {
21 * The formats that this Encoder supports.
25 protected static $format = ['json', 'ajax'];
30 public function __construct(JsonEncode $encodingImpl = NULL, JsonDecode $decodingImpl = NULL) {
31 // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be
32 // embedded into HTML.
33 // @see \Symfony\Component\HttpFoundation\JsonResponse
34 $json_encoding_options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
35 $this->encodingImpl = $encodingImpl ?: new JsonEncode($json_encoding_options);
36 $this->decodingImpl = $decodingImpl ?: new JsonDecode(TRUE);
42 public function supportsEncoding($format) {
43 return in_array($format, static::$format);
49 public function supportsDecoding($format) {
50 return in_array($format, static::$format);