1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Json; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Json is a helper class providing JSON data encoding and decoding. |
7
|
|
|
* It enhances the PHP built-in functions `json_encode()` and `json_decode()` |
8
|
|
|
* by throwing exceptions when decoding fails. |
9
|
|
|
*/ |
10
|
|
|
final class Json |
11
|
|
|
{ |
12
|
|
|
private const ERRORS = [ |
13
|
|
|
'JSON_ERROR_DEPTH' => 'Maximum stack depth exceeded', |
14
|
|
|
'JSON_ERROR_STATE_MISMATCH' => 'State mismatch (invalid or malformed JSON)', |
15
|
|
|
'JSON_ERROR_CTRL_CHAR' => 'Control character error, possibly incorrectly encoded', |
16
|
|
|
'JSON_ERROR_SYNTAX' => 'Syntax error', |
17
|
|
|
'JSON_ERROR_UTF8' => 'Malformed UTF-8 characters, possibly incorrectly encoded', |
18
|
|
|
'JSON_ERROR_RECURSION' => 'Recursion detected', |
19
|
|
|
'JSON_ERROR_INF_OR_NAN' => 'Inf and NaN cannot be JSON encoded', |
20
|
|
|
'JSON_ERROR_UNSUPPORTED_TYPE' => 'Type is not supported', |
21
|
|
|
'JSON_ERROR_INVALID_PROPERTY_NAME' => 'The decoded property name is invalid', |
22
|
|
|
'JSON_ERROR_UTF16' => 'Single unpaired UTF-16 surrogate in unicode escape', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Encodes the given value into a JSON string. |
27
|
|
|
* |
28
|
|
|
* Note that data encoded as JSON must be UTF-8 encoded according to the JSON specification. |
29
|
|
|
* You must ensure strings passed to this method have proper encoding before passing them. |
30
|
|
|
* |
31
|
|
|
* @param mixed $value the data to be encoded. |
32
|
|
|
* @param int $options the encoding options. For more details please refer to |
33
|
|
|
* <http://www.php.net/manual/en/function.json-encode.php>. Default is `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR`. |
34
|
|
|
* @param int $depth the maximum depth. |
35
|
|
|
* @return string the encoding result. |
36
|
|
|
* @throws \JsonException if there is any encoding error. |
37
|
|
|
*/ |
38
|
18 |
|
public static function encode( |
39
|
|
|
$value, |
40
|
|
|
int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR, |
41
|
|
|
int $depth = 512 |
42
|
|
|
): string { |
43
|
18 |
|
$shouldRethrowErrors = self::shouldRethrowErrors($options); |
44
|
|
|
|
45
|
18 |
|
$value = self::processData($value); |
46
|
|
|
|
47
|
18 |
|
if ($shouldRethrowErrors) { |
48
|
|
|
set_error_handler( |
49
|
|
|
static function () { |
50
|
|
|
self::rethrowJsonError(JSON_ERROR_SYNTAX); |
51
|
|
|
}, |
52
|
|
|
E_WARNING |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
18 |
|
$json = json_encode($value, $options, $depth); |
57
|
|
|
|
58
|
17 |
|
if ($shouldRethrowErrors) { |
59
|
|
|
restore_error_handler(); |
60
|
|
|
self::rethrowJsonError(json_last_error()); |
61
|
|
|
} |
62
|
|
|
|
63
|
17 |
|
return $json; |
64
|
|
|
} |
65
|
|
|
|
66
|
20 |
|
private static function shouldRethrowErrors(int $options): bool |
67
|
|
|
{ |
68
|
20 |
|
if (!self::hasFlag($options, JSON_THROW_ON_ERROR)) { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
20 |
|
return PHP_VERSION_ID < 70300; |
73
|
|
|
} |
74
|
|
|
|
75
|
20 |
|
private static function hasFlag(int $flags, int $flag): bool |
76
|
|
|
{ |
77
|
20 |
|
return ($flags & $flag) === $flag; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Encodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code. |
82
|
|
|
* |
83
|
|
|
* Note that data encoded as JSON must be UTF-8 encoded according to the JSON specification. |
84
|
|
|
* You must ensure strings passed to this method have proper encoding before passing them. |
85
|
|
|
* |
86
|
|
|
* @param mixed $value the data to be encoded |
87
|
|
|
* @return string the encoding result |
88
|
|
|
* @throws \JsonException if there is any encoding error |
89
|
|
|
*/ |
90
|
6 |
|
public static function htmlEncode($value): string |
91
|
|
|
{ |
92
|
6 |
|
return self::encode( |
93
|
6 |
|
$value, |
94
|
6 |
|
JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_THROW_ON_ERROR |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Decodes the given JSON string into a PHP data structure. |
100
|
|
|
* @param string $json the JSON string to be decoded |
101
|
|
|
* @param bool $asArray whether to return objects in terms of associative arrays. |
102
|
|
|
* @param int $depth the recursion depth. |
103
|
|
|
* @param int $options the decode options. |
104
|
|
|
* @return mixed the PHP data |
105
|
|
|
* @throws \JsonException if there is any decoding error |
106
|
|
|
*/ |
107
|
5 |
|
public static function decode( |
108
|
|
|
string $json, |
109
|
|
|
bool $asArray = true, |
110
|
|
|
int $depth = 512, |
111
|
|
|
int $options = JSON_THROW_ON_ERROR |
112
|
|
|
) { |
113
|
5 |
|
if ($json === '') { |
114
|
1 |
|
return null; |
115
|
|
|
} |
116
|
4 |
|
$decode = json_decode($json, $asArray, $depth, $options); |
117
|
|
|
|
118
|
2 |
|
if (self::shouldRethrowErrors($options)) { |
119
|
|
|
self::rethrowJsonError(json_last_error()); |
120
|
|
|
} |
121
|
2 |
|
return $decode; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Handles [[encode()]] and [[decode()]] errors by throwing exceptions with the respective error message. |
126
|
|
|
* |
127
|
|
|
* @param int $lastError error code from [json_last_error()](http://php.net/manual/en/function.json-last-error.php). |
128
|
|
|
* @throws \JsonException if there is any encoding/decoding error. |
129
|
|
|
*/ |
130
|
|
|
private static function rethrowJsonError(int $lastError): void |
131
|
|
|
{ |
132
|
|
|
if ($lastError === JSON_ERROR_NONE) { |
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
$availableErrors = []; |
136
|
|
|
foreach (self::ERRORS as $constant => $message) { |
137
|
|
|
if (defined($constant)) { |
138
|
|
|
$availableErrors[constant($constant)] = $message; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
if (isset($availableErrors[$lastError])) { |
142
|
|
|
throw new \JsonException($availableErrors[$lastError], $lastError); |
143
|
|
|
} |
144
|
|
|
throw new \JsonException('Unknown JSON encoding/decoding error.'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Pre-processes the data before sending it to `json_encode()`. |
149
|
|
|
* @param mixed $data the data to be processed |
150
|
|
|
* @return mixed the processed data |
151
|
|
|
*/ |
152
|
18 |
|
private static function processData($data) |
153
|
|
|
{ |
154
|
18 |
|
if (is_object($data)) { |
155
|
12 |
|
if ($data instanceof \JsonSerializable) { |
156
|
5 |
|
return self::processData($data->jsonSerialize()); |
157
|
|
|
} |
158
|
|
|
|
159
|
9 |
|
if ($data instanceof \DateTimeInterface) { |
160
|
1 |
|
return static::processData((array)$data); |
161
|
|
|
} |
162
|
|
|
|
163
|
8 |
|
if ($data instanceof \SimpleXMLElement) { |
164
|
1 |
|
$data = (array)$data; |
165
|
|
|
} else { |
166
|
7 |
|
$result = []; |
167
|
7 |
|
foreach ($data as $name => $value) { |
168
|
3 |
|
$result[$name] = $value; |
169
|
|
|
} |
170
|
7 |
|
$data = $result; |
171
|
|
|
} |
172
|
8 |
|
if ($data === []) { |
173
|
4 |
|
return new \stdClass(); |
174
|
|
|
} |
175
|
|
|
} |
176
|
15 |
|
if (is_array($data)) { |
177
|
12 |
|
foreach ($data as $key => $value) { |
178
|
10 |
|
if (is_array($value) || is_object($value)) { |
179
|
2 |
|
$data[$key] = self::processData($value); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
15 |
|
return $data; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|