12
12
use InvalidArgumentException ;
13
13
use RuntimeException ;
14
14
use stdClass ;
15
+ use Throwable ;
15
16
use function array_merge ;
16
17
use function basename ;
17
18
use function dirname ;
21
22
use function is_file ;
22
23
use function json_decode ;
23
24
use function json_encode ;
24
- use function json_last_error ;
25
- use function json_last_error_msg ;
26
25
use function preg_replace ;
27
26
use function trim ;
28
27
use const JSON_PRETTY_PRINT ;
@@ -45,11 +44,9 @@ class JsonHelper
45
44
* @param int $depth
46
45
*
47
46
* @return string
48
- * @noinspection PhpDocMissingThrowsInspection
49
47
*/
50
48
public static function enc (mixed $ data , int $ flags = 0 , int $ depth = 512 ): string
51
49
{
52
- /** @noinspection PhpUnhandledExceptionInspection */
53
50
return self ::encode ($ data , $ flags , $ depth );
54
51
}
55
52
@@ -61,12 +58,14 @@ public static function enc(mixed $data, int $flags = 0, int $depth = 512): strin
61
58
* @param int $depth
62
59
*
63
60
* @return string
64
- * @noinspection PhpDocMissingThrowsInspection
65
61
*/
66
62
public static function encode (mixed $ data , int $ options = 0 , int $ depth = 512 ): string
67
63
{
68
- /** @noinspection PhpUnhandledExceptionInspection */
69
- return (string )json_encode ($ data , JSON_THROW_ON_ERROR | $ options , $ depth );
64
+ try {
65
+ return (string )json_encode ($ data , JSON_THROW_ON_ERROR | $ options , $ depth );
66
+ } catch (Throwable $ e ) {
67
+ throw new RuntimeException ('JSON encode error - ' . $ e ->getMessage (), $ e ->getCode (), $ e );
68
+ }
70
69
}
71
70
72
71
/**
@@ -77,26 +76,22 @@ public static function encode(mixed $data, int $options = 0, int $depth = 512):
77
76
* @param int $depth
78
77
*
79
78
* @return string
80
- * @noinspection PhpDocMissingThrowsInspection
81
79
*/
82
80
public static function encodeCN (
83
81
mixed $ data ,
84
82
int $ options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ,
85
83
int $ depth = 512
86
84
): string {
87
- /** @noinspection PhpUnhandledExceptionInspection */
88
85
return self ::encode ($ data , $ options , $ depth );
89
86
}
90
87
91
88
/**
92
89
* @param $data
93
90
*
94
91
* @return string
95
- * @noinspection PhpDocMissingThrowsInspection
96
92
*/
97
93
public static function pretty ($ data ): string
98
94
{
99
- /** @noinspection PhpUnhandledExceptionInspection */
100
95
return self ::encode ($ data , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
101
96
}
102
97
@@ -105,49 +100,41 @@ public static function pretty($data): string
105
100
* @param int $flags
106
101
*
107
102
* @return string
108
- * @noinspection PhpDocMissingThrowsInspection
109
103
*/
110
104
public static function prettyJSON (
111
105
mixed $ data ,
112
106
int $ flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
113
107
): string {
114
- /** @noinspection PhpUnhandledExceptionInspection */
115
108
return self ::encode ($ data , $ flags );
116
109
}
117
110
118
111
/**
119
112
* @param $data
120
113
*
121
114
* @return string
122
- * @noinspection PhpDocMissingThrowsInspection
123
115
*/
124
116
public static function unescaped ($ data ): string
125
117
{
126
- /** @noinspection PhpUnhandledExceptionInspection */
127
118
return self ::encode ($ data , JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
128
119
}
129
120
130
121
/**
131
122
* @param $data
132
123
*
133
124
* @return string
134
- * @noinspection PhpDocMissingThrowsInspection
135
125
*/
136
126
public static function unescapedSlashes ($ data ): string
137
127
{
138
- /** @noinspection PhpUnhandledExceptionInspection */
139
128
return self ::encode ($ data , JSON_UNESCAPED_SLASHES );
140
129
}
141
130
142
131
/**
143
- * @param $data
132
+ * @param mixed $data
144
133
*
145
134
* @return string
146
- * @noinspection PhpDocMissingThrowsInspection
147
135
*/
148
- public static function unescapedUnicode ($ data ): string
136
+ public static function unescapedUnicode (mixed $ data ): string
149
137
{
150
- /** @noinspection PhpUnhandledExceptionInspection */
151
138
return self ::encode ($ data , JSON_UNESCAPED_UNICODE );
152
139
}
153
140
@@ -158,39 +145,28 @@ public static function unescapedUnicode($data): string
158
145
* @param bool $assoc
159
146
*
160
147
* @return array|stdClass
161
- * @noinspection PhpDocMissingThrowsInspection
162
148
*/
163
149
public static function dec (string $ json , bool $ assoc = true ): array |stdClass
164
150
{
165
- /** @noinspection PhpUnhandledExceptionInspection */
166
- $ data = json_decode ($ json , $ assoc , 512 , JSON_THROW_ON_ERROR );
167
-
168
- if (JSON_ERROR_NONE !== json_last_error ()) {
169
- throw new InvalidArgumentException ('json_decode error: ' . json_last_error_msg ());
170
- }
171
-
172
- return $ data ;
151
+ return self ::decode ($ json , $ assoc );
173
152
}
174
153
175
154
/**
176
- * Decode json
155
+ * Decode JSON string.
177
156
*
178
157
* @param string $json
179
158
* @param bool $assoc
180
159
* @param int $depth
181
160
* @param int $options
182
161
*
183
162
* @return array|object
184
- * @noinspection PhpDocMissingThrowsInspection
185
163
*/
186
164
public static function decode (string $ json , bool $ assoc = false , int $ depth = 512 , int $ options = 0 ): object |array
187
165
{
188
- /** @noinspection PhpUnhandledExceptionInspection */
189
- $ data = json_decode ($ json , $ assoc , $ depth , JSON_THROW_ON_ERROR | $ options );
190
-
191
- if ($ errCode = json_last_error ()) {
192
- $ errMsg = json_last_error_msg ();
193
- throw new RuntimeException ("JSON decode error: $ errMsg " , $ errCode );
166
+ try {
167
+ $ data = json_decode ($ json , $ assoc , $ depth , JSON_THROW_ON_ERROR | $ options );
168
+ } catch (Throwable $ e ) {
169
+ throw new RuntimeException ('JSON decode error - ' . $ e ->getMessage (), $ e ->getCode (), $ e );
194
170
}
195
171
196
172
return $ data ;
@@ -205,17 +181,15 @@ public static function decode(string $json, bool $assoc = false, int $depth = 51
205
181
* @param int $options
206
182
*
207
183
* @return array|object
208
- * @noinspection PhpDocMissingThrowsInspection
209
184
*/
210
185
public static function decodeFile (string $ jsonFile , bool $ assoc = false , int $ depth = 512 , int $ options = 0 ): object |array
211
186
{
212
187
if (!is_file ($ jsonFile )) {
213
- throw new InvalidArgumentException ("json file not found : $ jsonFile " );
188
+ throw new InvalidArgumentException ("JSON file not exists : $ jsonFile " );
214
189
}
215
190
216
191
$ json = file_get_contents ($ jsonFile );
217
192
218
- /** @noinspection PhpUnhandledExceptionInspection */
219
193
return self ::decode ($ json , $ assoc , $ depth , $ options );
220
194
}
221
195
@@ -255,7 +229,6 @@ public static function parseFile(string $jsonFile, bool $toArray = true): array|
255
229
* @param bool $toArray
256
230
*
257
231
* @return array|stdClass
258
- * @noinspection PhpDocMissingThrowsInspection
259
232
*/
260
233
public static function parseString (string $ json , bool $ toArray = true ): array |stdClass
261
234
{
@@ -264,18 +237,22 @@ public static function parseString(string $json, bool $toArray = true): array|st
264
237
}
265
238
266
239
$ json = self ::stripComments ($ json );
267
- /** @noinspection PhpUnhandledExceptionInspection */
268
240
return self ::decode ($ json , $ toArray );
269
241
}
270
242
271
243
/**
244
+ * Format JSON string.
245
+ *
246
+ * ```php
247
+ * $options = [
248
+ * 'type' => 'min' // 输出数据类型 min 压缩过的 raw 正常的
249
+ * 'file' => 'xx.json' // 输出文件路径;仅是文件名,则会取输入路径
250
+ * ]
251
+ * ```
252
+ *
272
253
* @param string $input JSON 数据
273
254
* @param bool $output 是否输出到文件, 默认返回格式化的数据
274
- * @param array $options 当 $output=true,此选项有效
275
- * $options = [
276
- * 'type' => 'min' // 输出数据类型 min 压缩过的 raw 正常的
277
- * 'file' => 'xx.json' // 输出文件路径;仅是文件名,则会取输入路径
278
- * ]
255
+ * @param array{type: string, file: string} $options 当 $output=true,此选项有效
279
256
*
280
257
* @return string
281
258
*/
@@ -326,7 +303,7 @@ public static function saveAs(string $data, string $output, array $options = [])
326
303
327
304
// 去掉空白
328
305
if ($ options ['type ' ] === 'min ' ) {
329
- $ data = preg_replace ('/(?!\w)\s*?(?!\w)/i ' , '' , $ data );
306
+ $ data = preg_replace ('/(?!\w)\s*?(?!\w)/ ' , '' , $ data );
330
307
}
331
308
332
309
return file_put_contents ($ file , $ data ) > 0 ;
0 commit comments