annotate core/modules/serialization/src/Encoder/JsonEncoder.php @ 0:4c8ae668cc8c
Initial import (non-working)
author |
Chris Cannam |
date |
Wed, 29 Nov 2017 16:09:58 +0000 |
parents |
|
children |
1fec387a4317 |
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@0
|
13 */
|
Chris@0
|
14 class JsonEncoder extends BaseJsonEncoder implements EncoderInterface, DecoderInterface {
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * The formats that this Encoder supports.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @var array
|
Chris@0
|
20 */
|
Chris@0
|
21 protected static $format = ['json', 'ajax'];
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * {@inheritdoc}
|
Chris@0
|
25 */
|
Chris@0
|
26 public function __construct(JsonEncode $encodingImpl = NULL, JsonDecode $decodingImpl = NULL) {
|
Chris@0
|
27 // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be
|
Chris@0
|
28 // embedded into HTML.
|
Chris@0
|
29 // @see \Symfony\Component\HttpFoundation\JsonResponse
|
Chris@0
|
30 $json_encoding_options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
|
Chris@0
|
31 $this->encodingImpl = $encodingImpl ?: new JsonEncode($json_encoding_options);
|
Chris@0
|
32 $this->decodingImpl = $decodingImpl ?: new JsonDecode(TRUE);
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * {@inheritdoc}
|
Chris@0
|
37 */
|
Chris@0
|
38 public function supportsEncoding($format) {
|
Chris@0
|
39 return in_array($format, static::$format);
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * {@inheritdoc}
|
Chris@0
|
44 */
|
Chris@0
|
45 public function supportsDecoding($format) {
|
Chris@0
|
46 return in_array($format, static::$format);
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 }
|