-
-
Notifications
You must be signed in to change notification settings - Fork 586
/
JsonSerializationVisitor.php
199 lines (169 loc) · 4.84 KB
/
JsonSerializationVisitor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
namespace JMS\Serializer;
use JMS\Serializer\Exception\NotAcceptableException;
use JMS\Serializer\Exception\RuntimeException;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Visitor\SerializationVisitorInterface;
final class JsonSerializationVisitor extends AbstractVisitor implements SerializationVisitorInterface
{
/**
* @var int
*/
private $options = JSON_PRESERVE_ZERO_FRACTION;
/**
* @var array
*/
private $dataStack = [];
/**
* @var \ArrayObject
*/
private $data;
public function __construct(
int $options = JSON_PRESERVE_ZERO_FRACTION
) {
$this->dataStack = [];
$this->options = $options;
}
/**
* {@inheritdoc}
*/
public function visitNull($data, array $type)
{
return null;
}
/**
* {@inheritdoc}
*/
public function visitString(string $data, array $type)
{
return $data;
}
/**
* {@inheritdoc}
*/
public function visitBoolean(bool $data, array $type)
{
return $data;
}
/**
* {@inheritdoc}
*/
public function visitInteger(int $data, array $type)
{
return $data;
}
/**
* {@inheritdoc}
*/
public function visitDouble(float $data, array $type)
{
return $data;
}
/**
* @param array $data
* @param array $type
*
* @return array|\ArrayObject
*/
public function visitArray(array $data, array $type)
{
\array_push($this->dataStack, $data);
$rs = isset($type['params'][1]) ? new \ArrayObject() : [];
$isList = isset($type['params'][0]) && !isset($type['params'][1]);
$elType = $this->getElementType($type);
foreach ($data as $k => $v) {
try {
$v = $this->navigator->accept($v, $elType);
} catch (NotAcceptableException $e) {
continue;
}
if ($isList) {
$rs[] = $v;
} else {
$rs[$k] = $v;
}
}
\array_pop($this->dataStack);
return $rs;
}
public function startVisitingObject(ClassMetadata $metadata, object $data, array $type): void
{
\array_push($this->dataStack, $this->data);
$this->data = true === $metadata->isMap ? new \ArrayObject() : [];
}
/**
* @return array|\ArrayObject
*/
public function endVisitingObject(ClassMetadata $metadata, object $data, array $type)
{
$rs = $this->data;
$this->data = \array_pop($this->dataStack);
if (true !== $metadata->isList && empty($rs)) {
return new \ArrayObject();
}
return $rs;
}
/**
* {@inheritdoc}
*/
public function visitProperty(PropertyMetadata $metadata, $v): void
{
try {
$v = $this->navigator->accept($v, $metadata->type);
} catch (NotAcceptableException $e) {
return;
}
if (true === $metadata->skipWhenEmpty && ($v instanceof \ArrayObject || \is_array($v)) && 0 === count($v)) {
return;
}
if ($metadata->inline) {
if (\is_array($v) || ($v instanceof \ArrayObject)) {
// concatenate the two array-like structures
// is there anything faster?
foreach ($v as $key => $value) {
$this->data[$key] = $value;
}
}
} else {
$this->data[$metadata->serializedName] = $v;
}
}
/**
* @deprecated Will be removed in 3.0
*
* Checks if some data key exists.
*/
public function hasData(string $key): bool
{
return isset($this->data[$key]);
}
/**
* @deprecated Use visitProperty(new StaticPropertyMetadata(null, 'name', 'value'), null) instead
*
* Allows you to replace existing data on the current object element.
*
* @param mixed $value This value must either be a regular scalar, or an array.
* It must not contain any objects anymore.
*/
public function setData(string $key, $value): void
{
$this->data[$key] = $value;
}
/**
* {@inheritdoc}
*/
public function getResult($data)
{
$result = @json_encode($data, $this->options);
switch (json_last_error()) {
case JSON_ERROR_NONE:
return $result;
case JSON_ERROR_UTF8:
throw new RuntimeException('Your data could not be encoded because it contains invalid UTF8 characters.');
default:
throw new RuntimeException(sprintf('An error occurred while encoding your data (error code %d).', json_last_error()));
}
}
}