14
14
use stdClass ;
15
15
use function array_merge ;
16
16
use function basename ;
17
+ use function defined ;
17
18
use function dirname ;
18
19
use function file_exists ;
19
20
use function file_get_contents ;
29
30
use const JSON_UNESCAPED_SLASHES ;
30
31
use const JSON_UNESCAPED_UNICODE ;
31
32
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
+
32
39
/**
33
40
* Class JsonHelper
34
41
*
@@ -40,14 +47,16 @@ class JsonHelper
40
47
41
48
/**
42
49
* @param mixed $data
43
- * @param int $flags
44
- * @param int $depth
50
+ * @param int $flags
51
+ * @param int $depth
45
52
*
46
53
* @return string
54
+ * @noinspection PhpDocMissingThrowsInspection
47
55
*/
48
56
public static function enc ($ data , int $ flags = 0 , int $ depth = 512 ): string
49
57
{
50
- return json_encode ($ data , $ flags , $ depth );
58
+ /** @noinspection PhpUnhandledExceptionInspection */
59
+ return self ::encode ($ data , $ flags , $ depth );
51
60
}
52
61
53
62
/**
@@ -58,10 +67,12 @@ public static function enc($data, int $flags = 0, int $depth = 512): string
58
67
* @param int $depth
59
68
*
60
69
* @return string
70
+ * @noinspection PhpDocMissingThrowsInspection
61
71
*/
62
72
public static function encode ($ data , int $ options = 0 , int $ depth = 512 ): string
63
73
{
64
- return json_encode ($ data , $ options , $ depth );
74
+ /** @noinspection PhpUnhandledExceptionInspection */
75
+ return (string )json_encode ($ data , \JSON_THROW_ON_ERROR | $ options , $ depth );
65
76
}
66
77
67
78
/**
@@ -72,66 +83,78 @@ public static function encode($data, int $options = 0, int $depth = 512): string
72
83
* @param int $depth
73
84
*
74
85
* @return string
86
+ * @noinspection PhpDocMissingThrowsInspection
75
87
*/
76
88
public static function encodeCN (
77
89
$ data ,
78
90
int $ options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ,
79
91
int $ depth = 512
80
92
): string {
81
- return json_encode ($ data , $ options , $ depth );
93
+ /** @noinspection PhpUnhandledExceptionInspection */
94
+ return self ::encode ($ data , $ options , $ depth );
82
95
}
83
96
84
97
/**
85
98
* @param $data
86
99
*
87
100
* @return string
101
+ * @noinspection PhpDocMissingThrowsInspection
88
102
*/
89
103
public static function pretty ($ data ): string
90
104
{
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 );
92
107
}
93
108
94
109
/**
95
110
* @param mixed $data
96
111
* @param int $flags
97
112
*
98
- * @return false|string
113
+ * @return string
114
+ * @noinspection PhpDocMissingThrowsInspection
99
115
*/
100
116
public static function prettyJSON (
101
117
$ data ,
102
118
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 );
105
122
}
106
123
107
124
/**
108
125
* @param $data
109
126
*
110
127
* @return string
128
+ * @noinspection PhpDocMissingThrowsInspection
111
129
*/
112
130
public static function unescaped ($ data ): string
113
131
{
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 );
115
134
}
116
135
117
136
/**
118
137
* @param $data
119
138
*
120
139
* @return string
140
+ * @noinspection PhpDocMissingThrowsInspection
121
141
*/
122
142
public static function unescapedSlashes ($ data ): string
123
143
{
124
- return json_encode ($ data , JSON_UNESCAPED_SLASHES );
144
+ /** @noinspection PhpUnhandledExceptionInspection */
145
+ return self ::encode ($ data , JSON_UNESCAPED_SLASHES );
125
146
}
126
147
127
148
/**
128
149
* @param $data
129
150
*
130
151
* @return string
152
+ * @noinspection PhpDocMissingThrowsInspection
131
153
*/
132
154
public static function unescapedUnicode ($ data ): string
133
155
{
134
- return json_encode ($ data , JSON_UNESCAPED_UNICODE );
156
+ /** @noinspection PhpUnhandledExceptionInspection */
157
+ return self ::encode ($ data , JSON_UNESCAPED_UNICODE );
135
158
}
136
159
137
160
// ----------- decode -----------
@@ -142,10 +165,12 @@ public static function unescapedUnicode($data): string
142
165
* @param bool $assoc
143
166
*
144
167
* @return array|mixed
168
+ * @noinspection PhpDocMissingThrowsInspection
145
169
*/
146
170
public static function dec (string $ json , bool $ assoc = true )
147
171
{
148
- $ data = json_decode ($ json , $ assoc );
172
+ /** @noinspection PhpUnhandledExceptionInspection */
173
+ $ data = json_decode ($ json , $ assoc , 512 , JSON_THROW_ON_ERROR );
149
174
150
175
if (JSON_ERROR_NONE !== json_last_error ()) {
151
176
throw new InvalidArgumentException ('json_decode error: ' . json_last_error_msg ());
@@ -163,10 +188,12 @@ public static function dec(string $json, bool $assoc = true)
163
188
* @param int $options
164
189
*
165
190
* @return array|object
191
+ * @noinspection PhpDocMissingThrowsInspection
166
192
*/
167
193
public static function decode (string $ json , bool $ assoc = false , int $ depth = 512 , int $ options = 0 )
168
194
{
169
- $ data = json_decode ($ json , $ assoc , $ depth , $ options );
195
+ /** @noinspection PhpUnhandledExceptionInspection */
196
+ $ data = json_decode ($ json , $ assoc , $ depth , JSON_THROW_ON_ERROR | $ options );
170
197
171
198
if ($ errCode = json_last_error ()) {
172
199
$ errMsg = json_last_error_msg ();
@@ -185,24 +212,25 @@ public static function decode(string $json, bool $assoc = false, int $depth = 51
185
212
* @param int $options
186
213
*
187
214
* @return array|object
215
+ * @noinspection PhpDocMissingThrowsInspection
188
216
*/
189
217
public static function decodeFile (string $ jsonFile , bool $ assoc = false , int $ depth = 512 , int $ options = 0 )
190
218
{
191
219
if (!is_file ($ jsonFile )) {
192
- throw new InvalidArgumentException ("json file not found: { $ jsonFile} " );
220
+ throw new InvalidArgumentException ("json file not found: $ jsonFile " );
193
221
}
194
222
195
223
$ json = file_get_contents ($ jsonFile );
196
224
225
+ /** @noinspection PhpUnhandledExceptionInspection */
197
226
return self ::decode ($ json , $ assoc , $ depth , $ options );
198
227
}
199
228
200
229
/**
201
230
* @param string $data
202
231
* @param bool $toArray
203
232
*
204
- * @return array|mixed|stdClass
205
- * @throws InvalidArgumentException
233
+ * @return array|stdClass
206
234
*/
207
235
public static function parse (string $ data , bool $ toArray = true )
208
236
{
@@ -217,12 +245,12 @@ public static function parse(string $data, bool $toArray = true)
217
245
* @param string $jsonFile
218
246
* @param bool $toArray
219
247
*
220
- * @return array|mixed| stdClass
248
+ * @return array|stdClass
221
249
*/
222
250
public static function parseFile (string $ jsonFile , bool $ toArray = true )
223
251
{
224
252
if (!is_file ($ jsonFile )) {
225
- throw new InvalidArgumentException ("File not found: { $ jsonFile} " );
253
+ throw new InvalidArgumentException ("File not found: $ jsonFile " );
226
254
}
227
255
228
256
$ json = file_get_contents ($ jsonFile );
@@ -233,7 +261,8 @@ public static function parseFile(string $jsonFile, bool $toArray = true)
233
261
* @param string $json
234
262
* @param bool $toArray
235
263
*
236
- * @return array|mixed|stdClass
264
+ * @return array|stdClass
265
+ * @noinspection PhpDocMissingThrowsInspection
237
266
*/
238
267
public static function parseString (string $ json , bool $ toArray = true )
239
268
{
@@ -242,6 +271,7 @@ public static function parseString(string $json, bool $toArray = true)
242
271
}
243
272
244
273
$ json = self ::stripComments ($ json );
274
+ /** @noinspection PhpUnhandledExceptionInspection */
245
275
return self ::decode ($ json , $ toArray );
246
276
}
247
277
@@ -286,9 +316,9 @@ public static function format(string $input, bool $output = false, array $option
286
316
* @param string $output
287
317
* @param array $options
288
318
*
289
- * @return bool|int
319
+ * @return bool
290
320
*/
291
- public static function saveAs (string $ data , string $ output , array $ options = [])
321
+ public static function saveAs (string $ data , string $ output , array $ options = []): bool
292
322
{
293
323
$ default = ['type ' => 'min ' , 'file ' => '' ];
294
324
$ options = array_merge ($ default , $ options );
@@ -306,7 +336,7 @@ public static function saveAs(string $data, string $output, array $options = [])
306
336
$ data = preg_replace ('/(?!\w)\s*?(?!\w)/i ' , '' , $ data );
307
337
}
308
338
309
- return file_put_contents ($ file , $ data );
339
+ return file_put_contents ($ file , $ data ) > 0 ;
310
340
}
311
341
312
342
/**
0 commit comments