1414use stdClass ;
1515use function array_merge ;
1616use function basename ;
17+ use function defined ;
1718use function dirname ;
1819use function file_exists ;
1920use function file_get_contents ;
2930use const JSON_UNESCAPED_SLASHES ;
3031use const JSON_UNESCAPED_UNICODE ;
3132
33+ // Compatible with lower versions
34+ if (!defined ('JSON_THROW_ON_ERROR ' )) {
35+ define ('JSON_THROW_ON_ERROR ' , 4194304 ); // since php 7.3
36+ // class JsonException extends RuntimeException {}
37+ }
38+
3239/**
3340 * Class JsonHelper
3441 *
@@ -40,14 +47,16 @@ class JsonHelper
4047
4148 /**
4249 * @param mixed $data
43- * @param int $flags
44- * @param int $depth
50+ * @param int $flags
51+ * @param int $depth
4552 *
4653 * @return string
54+ * @noinspection PhpDocMissingThrowsInspection
4755 */
4856 public static function enc ($ data , int $ flags = 0 , int $ depth = 512 ): string
4957 {
50- return json_encode ($ data , $ flags , $ depth );
58+ /** @noinspection PhpUnhandledExceptionInspection */
59+ return self ::encode ($ data , $ flags , $ depth );
5160 }
5261
5362 /**
@@ -58,10 +67,12 @@ public static function enc($data, int $flags = 0, int $depth = 512): string
5867 * @param int $depth
5968 *
6069 * @return string
70+ * @noinspection PhpDocMissingThrowsInspection
6171 */
6272 public static function encode ($ data , int $ options = 0 , int $ depth = 512 ): string
6373 {
64- return json_encode ($ data , $ options , $ depth );
74+ /** @noinspection PhpUnhandledExceptionInspection */
75+ return (string )json_encode ($ data , \JSON_THROW_ON_ERROR | $ options , $ depth );
6576 }
6677
6778 /**
@@ -72,66 +83,78 @@ public static function encode($data, int $options = 0, int $depth = 512): string
7283 * @param int $depth
7384 *
7485 * @return string
86+ * @noinspection PhpDocMissingThrowsInspection
7587 */
7688 public static function encodeCN (
7789 $ data ,
7890 int $ options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ,
7991 int $ depth = 512
8092 ): string {
81- return json_encode ($ data , $ options , $ depth );
93+ /** @noinspection PhpUnhandledExceptionInspection */
94+ return self ::encode ($ data , $ options , $ depth );
8295 }
8396
8497 /**
8598 * @param $data
8699 *
87100 * @return string
101+ * @noinspection PhpDocMissingThrowsInspection
88102 */
89103 public static function pretty ($ data ): string
90104 {
91- return json_encode ($ data , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
105+ /** @noinspection PhpUnhandledExceptionInspection */
106+ return self ::encode ($ data , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
92107 }
93108
94109 /**
95110 * @param mixed $data
96111 * @param int $flags
97112 *
98- * @return false|string
113+ * @return string
114+ * @noinspection PhpDocMissingThrowsInspection
99115 */
100116 public static function prettyJSON (
101117 $ data ,
102118 int $ flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
103- ) {
104- return json_encode ($ data , $ flags );
119+ ): string {
120+ /** @noinspection PhpUnhandledExceptionInspection */
121+ return self ::encode ($ data , $ flags );
105122 }
106123
107124 /**
108125 * @param $data
109126 *
110127 * @return string
128+ * @noinspection PhpDocMissingThrowsInspection
111129 */
112130 public static function unescaped ($ data ): string
113131 {
114- return json_encode ($ data , JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
132+ /** @noinspection PhpUnhandledExceptionInspection */
133+ return self ::encode ($ data , JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
115134 }
116135
117136 /**
118137 * @param $data
119138 *
120139 * @return string
140+ * @noinspection PhpDocMissingThrowsInspection
121141 */
122142 public static function unescapedSlashes ($ data ): string
123143 {
124- return json_encode ($ data , JSON_UNESCAPED_SLASHES );
144+ /** @noinspection PhpUnhandledExceptionInspection */
145+ return self ::encode ($ data , JSON_UNESCAPED_SLASHES );
125146 }
126147
127148 /**
128149 * @param $data
129150 *
130151 * @return string
152+ * @noinspection PhpDocMissingThrowsInspection
131153 */
132154 public static function unescapedUnicode ($ data ): string
133155 {
134- return json_encode ($ data , JSON_UNESCAPED_UNICODE );
156+ /** @noinspection PhpUnhandledExceptionInspection */
157+ return self ::encode ($ data , JSON_UNESCAPED_UNICODE );
135158 }
136159
137160 // ----------- decode -----------
@@ -142,10 +165,12 @@ public static function unescapedUnicode($data): string
142165 * @param bool $assoc
143166 *
144167 * @return array|mixed
168+ * @noinspection PhpDocMissingThrowsInspection
145169 */
146170 public static function dec (string $ json , bool $ assoc = true )
147171 {
148- $ data = json_decode ($ json , $ assoc );
172+ /** @noinspection PhpUnhandledExceptionInspection */
173+ $ data = json_decode ($ json , $ assoc , 512 , JSON_THROW_ON_ERROR );
149174
150175 if (JSON_ERROR_NONE !== json_last_error ()) {
151176 throw new InvalidArgumentException ('json_decode error: ' . json_last_error_msg ());
@@ -163,10 +188,12 @@ public static function dec(string $json, bool $assoc = true)
163188 * @param int $options
164189 *
165190 * @return array|object
191+ * @noinspection PhpDocMissingThrowsInspection
166192 */
167193 public static function decode (string $ json , bool $ assoc = false , int $ depth = 512 , int $ options = 0 )
168194 {
169- $ data = json_decode ($ json , $ assoc , $ depth , $ options );
195+ /** @noinspection PhpUnhandledExceptionInspection */
196+ $ data = json_decode ($ json , $ assoc , $ depth , JSON_THROW_ON_ERROR | $ options );
170197
171198 if ($ errCode = json_last_error ()) {
172199 $ errMsg = json_last_error_msg ();
@@ -185,24 +212,25 @@ public static function decode(string $json, bool $assoc = false, int $depth = 51
185212 * @param int $options
186213 *
187214 * @return array|object
215+ * @noinspection PhpDocMissingThrowsInspection
188216 */
189217 public static function decodeFile (string $ jsonFile , bool $ assoc = false , int $ depth = 512 , int $ options = 0 )
190218 {
191219 if (!is_file ($ jsonFile )) {
192- throw new InvalidArgumentException ("json file not found: { $ jsonFile} " );
220+ throw new InvalidArgumentException ("json file not found: $ jsonFile " );
193221 }
194222
195223 $ json = file_get_contents ($ jsonFile );
196224
225+ /** @noinspection PhpUnhandledExceptionInspection */
197226 return self ::decode ($ json , $ assoc , $ depth , $ options );
198227 }
199228
200229 /**
201230 * @param string $data
202231 * @param bool $toArray
203232 *
204- * @return array|mixed|stdClass
205- * @throws InvalidArgumentException
233+ * @return array|stdClass
206234 */
207235 public static function parse (string $ data , bool $ toArray = true )
208236 {
@@ -217,12 +245,12 @@ public static function parse(string $data, bool $toArray = true)
217245 * @param string $jsonFile
218246 * @param bool $toArray
219247 *
220- * @return array|mixed| stdClass
248+ * @return array|stdClass
221249 */
222250 public static function parseFile (string $ jsonFile , bool $ toArray = true )
223251 {
224252 if (!is_file ($ jsonFile )) {
225- throw new InvalidArgumentException ("File not found: { $ jsonFile} " );
253+ throw new InvalidArgumentException ("File not found: $ jsonFile " );
226254 }
227255
228256 $ json = file_get_contents ($ jsonFile );
@@ -233,7 +261,8 @@ public static function parseFile(string $jsonFile, bool $toArray = true)
233261 * @param string $json
234262 * @param bool $toArray
235263 *
236- * @return array|mixed|stdClass
264+ * @return array|stdClass
265+ * @noinspection PhpDocMissingThrowsInspection
237266 */
238267 public static function parseString (string $ json , bool $ toArray = true )
239268 {
@@ -242,6 +271,7 @@ public static function parseString(string $json, bool $toArray = true)
242271 }
243272
244273 $ json = self ::stripComments ($ json );
274+ /** @noinspection PhpUnhandledExceptionInspection */
245275 return self ::decode ($ json , $ toArray );
246276 }
247277
@@ -286,9 +316,9 @@ public static function format(string $input, bool $output = false, array $option
286316 * @param string $output
287317 * @param array $options
288318 *
289- * @return bool|int
319+ * @return bool
290320 */
291- public static function saveAs (string $ data , string $ output , array $ options = [])
321+ public static function saveAs (string $ data , string $ output , array $ options = []): bool
292322 {
293323 $ default = ['type ' => 'min ' , 'file ' => '' ];
294324 $ options = array_merge ($ default , $ options );
@@ -306,7 +336,7 @@ public static function saveAs(string $data, string $output, array $options = [])
306336 $ data = preg_replace ('/(?!\w)\s*?(?!\w)/i ' , '' , $ data );
307337 }
308338
309- return file_put_contents ($ file , $ data );
339+ return file_put_contents ($ file , $ data ) > 0 ;
310340 }
311341
312342 /**
0 commit comments