1212use InvalidArgumentException ;
1313use RuntimeException ;
1414use stdClass ;
15+ use Throwable ;
1516use function array_merge ;
1617use function basename ;
1718use function dirname ;
2122use function is_file ;
2223use function json_decode ;
2324use function json_encode ;
24- use function json_last_error ;
25- use function json_last_error_msg ;
2625use function preg_replace ;
2726use function trim ;
2827use const JSON_PRETTY_PRINT ;
@@ -45,11 +44,9 @@ class JsonHelper
4544 * @param int $depth
4645 *
4746 * @return string
48- * @noinspection PhpDocMissingThrowsInspection
4947 */
5048 public static function enc (mixed $ data , int $ flags = 0 , int $ depth = 512 ): string
5149 {
52- /** @noinspection PhpUnhandledExceptionInspection */
5350 return self ::encode ($ data , $ flags , $ depth );
5451 }
5552
@@ -61,12 +58,14 @@ public static function enc(mixed $data, int $flags = 0, int $depth = 512): strin
6158 * @param int $depth
6259 *
6360 * @return string
64- * @noinspection PhpDocMissingThrowsInspection
6561 */
6662 public static function encode (mixed $ data , int $ options = 0 , int $ depth = 512 ): string
6763 {
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+ }
7069 }
7170
7271 /**
@@ -77,26 +76,22 @@ public static function encode(mixed $data, int $options = 0, int $depth = 512):
7776 * @param int $depth
7877 *
7978 * @return string
80- * @noinspection PhpDocMissingThrowsInspection
8179 */
8280 public static function encodeCN (
8381 mixed $ data ,
8482 int $ options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ,
8583 int $ depth = 512
8684 ): string {
87- /** @noinspection PhpUnhandledExceptionInspection */
8885 return self ::encode ($ data , $ options , $ depth );
8986 }
9087
9188 /**
9289 * @param $data
9390 *
9491 * @return string
95- * @noinspection PhpDocMissingThrowsInspection
9692 */
9793 public static function pretty ($ data ): string
9894 {
99- /** @noinspection PhpUnhandledExceptionInspection */
10095 return self ::encode ($ data , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
10196 }
10297
@@ -105,49 +100,41 @@ public static function pretty($data): string
105100 * @param int $flags
106101 *
107102 * @return string
108- * @noinspection PhpDocMissingThrowsInspection
109103 */
110104 public static function prettyJSON (
111105 mixed $ data ,
112106 int $ flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
113107 ): string {
114- /** @noinspection PhpUnhandledExceptionInspection */
115108 return self ::encode ($ data , $ flags );
116109 }
117110
118111 /**
119112 * @param $data
120113 *
121114 * @return string
122- * @noinspection PhpDocMissingThrowsInspection
123115 */
124116 public static function unescaped ($ data ): string
125117 {
126- /** @noinspection PhpUnhandledExceptionInspection */
127118 return self ::encode ($ data , JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
128119 }
129120
130121 /**
131122 * @param $data
132123 *
133124 * @return string
134- * @noinspection PhpDocMissingThrowsInspection
135125 */
136126 public static function unescapedSlashes ($ data ): string
137127 {
138- /** @noinspection PhpUnhandledExceptionInspection */
139128 return self ::encode ($ data , JSON_UNESCAPED_SLASHES );
140129 }
141130
142131 /**
143- * @param $data
132+ * @param mixed $data
144133 *
145134 * @return string
146- * @noinspection PhpDocMissingThrowsInspection
147135 */
148- public static function unescapedUnicode ($ data ): string
136+ public static function unescapedUnicode (mixed $ data ): string
149137 {
150- /** @noinspection PhpUnhandledExceptionInspection */
151138 return self ::encode ($ data , JSON_UNESCAPED_UNICODE );
152139 }
153140
@@ -158,39 +145,28 @@ public static function unescapedUnicode($data): string
158145 * @param bool $assoc
159146 *
160147 * @return array|stdClass
161- * @noinspection PhpDocMissingThrowsInspection
162148 */
163149 public static function dec (string $ json , bool $ assoc = true ): array |stdClass
164150 {
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 );
173152 }
174153
175154 /**
176- * Decode json
155+ * Decode JSON string.
177156 *
178157 * @param string $json
179158 * @param bool $assoc
180159 * @param int $depth
181160 * @param int $options
182161 *
183162 * @return array|object
184- * @noinspection PhpDocMissingThrowsInspection
185163 */
186164 public static function decode (string $ json , bool $ assoc = false , int $ depth = 512 , int $ options = 0 ): object |array
187165 {
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 );
194170 }
195171
196172 return $ data ;
@@ -205,17 +181,15 @@ public static function decode(string $json, bool $assoc = false, int $depth = 51
205181 * @param int $options
206182 *
207183 * @return array|object
208- * @noinspection PhpDocMissingThrowsInspection
209184 */
210185 public static function decodeFile (string $ jsonFile , bool $ assoc = false , int $ depth = 512 , int $ options = 0 ): object |array
211186 {
212187 if (!is_file ($ jsonFile )) {
213- throw new InvalidArgumentException ("json file not found : $ jsonFile " );
188+ throw new InvalidArgumentException ("JSON file not exists : $ jsonFile " );
214189 }
215190
216191 $ json = file_get_contents ($ jsonFile );
217192
218- /** @noinspection PhpUnhandledExceptionInspection */
219193 return self ::decode ($ json , $ assoc , $ depth , $ options );
220194 }
221195
@@ -255,7 +229,6 @@ public static function parseFile(string $jsonFile, bool $toArray = true): array|
255229 * @param bool $toArray
256230 *
257231 * @return array|stdClass
258- * @noinspection PhpDocMissingThrowsInspection
259232 */
260233 public static function parseString (string $ json , bool $ toArray = true ): array |stdClass
261234 {
@@ -264,18 +237,22 @@ public static function parseString(string $json, bool $toArray = true): array|st
264237 }
265238
266239 $ json = self ::stripComments ($ json );
267- /** @noinspection PhpUnhandledExceptionInspection */
268240 return self ::decode ($ json , $ toArray );
269241 }
270242
271243 /**
244+ * Format JSON string.
245+ *
246+ * ```php
247+ * $options = [
248+ * 'type' => 'min' // 输出数据类型 min 压缩过的 raw 正常的
249+ * 'file' => 'xx.json' // 输出文件路径;仅是文件名,则会取输入路径
250+ * ]
251+ * ```
252+ *
272253 * @param string $input JSON 数据
273254 * @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,此选项有效
279256 *
280257 * @return string
281258 */
@@ -326,7 +303,7 @@ public static function saveAs(string $data, string $output, array $options = [])
326303
327304 // 去掉空白
328305 if ($ options ['type ' ] === 'min ' ) {
329- $ data = preg_replace ('/(?!\w)\s*?(?!\w)/i ' , '' , $ data );
306+ $ data = preg_replace ('/(?!\w)\s*?(?!\w)/ ' , '' , $ data );
330307 }
331308
332309 return file_put_contents ($ file , $ data ) > 0 ;
0 commit comments