1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Serializer; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\Serializer\Encoder\DecoderInterface; |
17
|
|
|
use Symfony\Component\Serializer\Encoder\EncoderInterface; |
18
|
|
|
use Symfony\Component\Serializer\Encoder\JsonDecode; |
19
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncode; |
20
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder as BaseJsonEncoder; |
21
|
|
|
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A JSON encoder with appropriate default options to embed the generated document into HTML. |
25
|
|
|
* |
26
|
|
|
* @author Kévin Dunglas <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class JsonEncoder implements EncoderInterface, DecoderInterface |
29
|
|
|
{ |
30
|
|
|
private $format; |
31
|
|
|
private $jsonEncoder; |
32
|
|
|
|
33
|
|
|
public function __construct(string $format, BaseJsonEncoder $jsonEncoder = null) |
34
|
|
|
{ |
35
|
|
|
$this->format = $format; |
36
|
|
|
$this->jsonEncoder = $jsonEncoder; |
37
|
|
|
|
38
|
|
|
if (null !== $this->jsonEncoder) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Encode <, >, ', &, and " characters in the JSON, making it also safe to be embedded into HTML. |
43
|
|
|
$jsonEncodeOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE; |
44
|
|
|
if (interface_exists(AdvancedNameConverterInterface::class)) { |
45
|
|
|
$jsonEncode = new JsonEncode(['json_encode_options' => $jsonEncodeOptions]); |
46
|
|
|
$jsonDecode = new JsonDecode(['json_decode_associative' => true]); |
47
|
|
|
} else { |
48
|
|
|
$jsonEncode = new JsonEncode($jsonEncodeOptions); |
|
|
|
|
49
|
|
|
$jsonDecode = new JsonDecode(true); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->jsonEncoder = new BaseJsonEncoder($jsonEncode, $jsonDecode); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function supportsEncoding($format) |
59
|
|
|
{ |
60
|
|
|
return $this->format === $format; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function encode($data, $format, array $context = []) |
67
|
|
|
{ |
68
|
|
|
return $this->jsonEncoder->encode($data, $format, $context); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function supportsDecoding($format) |
75
|
|
|
{ |
76
|
|
|
return $this->format === $format; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function decode($data, $format, array $context = []) |
83
|
|
|
{ |
84
|
|
|
return $this->jsonEncoder->decode($data, $format, $context); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|