19
19
use function file_get_contents ;
20
20
use function file_put_contents ;
21
21
use function is_file ;
22
- use function is_string ;
23
22
use function json_decode ;
24
23
use function json_encode ;
25
24
use function json_last_error ;
@@ -54,8 +53,8 @@ public static function prettyJSON(
54
53
* Encode data to json
55
54
*
56
55
* @param mixed $data
57
- * @param int $options
58
- * @param int $depth
56
+ * @param int $options
57
+ * @param int $depth
59
58
*
60
59
* @return string
61
60
*/
@@ -68,13 +67,16 @@ public static function encode($data, int $options = 0, int $depth = 512): string
68
67
* Encode data to json with some default options
69
68
*
70
69
* @param mixed $data
71
- * @param int $options
72
- * @param int $depth
70
+ * @param int $options
71
+ * @param int $depth
73
72
*
74
73
* @return string
75
74
*/
76
- public static function encodeCN ($ data , int $ options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE , int $ depth = 512 ): string
77
- {
75
+ public static function encodeCN (
76
+ $ data ,
77
+ int $ options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ,
78
+ int $ depth = 512
79
+ ): string {
78
80
return json_encode ($ data , $ options , $ depth );
79
81
}
80
82
@@ -100,11 +102,32 @@ public static function decode(string $json, bool $assoc = false, int $depth = 51
100
102
return $ data ;
101
103
}
102
104
105
+ /**
106
+ * Decode json file
107
+ *
108
+ * @param string $jsonFile
109
+ * @param bool $assoc
110
+ * @param int $depth
111
+ * @param int $options
112
+ *
113
+ * @return array|object
114
+ */
115
+ public static function decodeFile (string $ jsonFile , bool $ assoc = false , int $ depth = 512 , int $ options = 0 )
116
+ {
117
+ if (!is_file ($ jsonFile )) {
118
+ throw new InvalidArgumentException ("json file not found: {$ jsonFile }" );
119
+ }
120
+
121
+ $ json = file_get_contents ($ jsonFile );
122
+
123
+ return self ::decode ($ json , $ assoc , $ depth , $ options );
124
+ }
125
+
103
126
/**
104
127
* @param string $data
105
128
* @param bool $toArray
106
129
*
107
- * @return array|mixed|null| stdClass|string
130
+ * @return array|mixed|stdClass
108
131
* @throws InvalidArgumentException
109
132
*/
110
133
public static function parse (string $ data , bool $ toArray = true )
@@ -117,83 +140,54 @@ public static function parse(string $data, bool $toArray = true)
117
140
}
118
141
119
142
/**
120
- * @param string $file
121
- * @param bool|true $toArray
143
+ * @param string $jsonFile
144
+ * @param bool $toArray
122
145
*
123
- * @return mixed|null|string
124
- * @throws InvalidArgumentException
146
+ * @return array|mixed|stdClass
125
147
*/
126
- public static function parseFile (string $ file , $ toArray = true )
148
+ public static function parseFile (string $ jsonFile , bool $ toArray = true )
127
149
{
128
- if (!is_file ($ file )) {
129
- throw new InvalidArgumentException ("File not found: {$ file }" );
150
+ if (!is_file ($ jsonFile )) {
151
+ throw new InvalidArgumentException ("File not found: {$ jsonFile }" );
130
152
}
131
153
132
- $ string = file_get_contents ($ file );
133
-
134
- return self ::parseString ($ string , $ toArray );
154
+ $ json = file_get_contents ($ jsonFile );
155
+ return self ::parseString ($ json , $ toArray );
135
156
}
136
157
137
158
/**
138
- * @param string $string
159
+ * @param string $json
139
160
* @param bool $toArray
140
161
*
141
162
* @return array|mixed|stdClass
142
163
*/
143
- public static function parseString (string $ string , bool $ toArray = true )
164
+ public static function parseString (string $ json , bool $ toArray = true )
144
165
{
145
- if (!$ string = trim ($ string )) {
166
+ if (!$ json = trim ($ json )) {
146
167
return $ toArray ? [] : new stdClass ();
147
168
}
148
169
149
- $ string = (string )preg_replace ([
150
- // 去掉所有多行注释/* .... */
151
- '/\/\*.*?\*\/\s*/is ' ,
152
- // 去掉所有单行注释//....
153
- '/\/\/.*?[\r\n]/is ' ,
154
- // 去掉空白, 多个空格换成一个
155
- //'/(?!\w)\s*?(?!\w)/is'
156
- ], ['' , '' , ' ' ], $ string );
157
-
158
- // json_last_error() === JSON_ERROR_NONE
159
- return json_decode ($ string , $ toArray );
170
+ $ json = self ::stripComments ($ json );
171
+ return self ::decode ($ json , $ toArray );
160
172
}
161
173
162
174
/**
163
- * @param string $input 文件 或 数据
175
+ * @param string $input JSON 数据
164
176
* @param bool $output 是否输出到文件, 默认返回格式化的数据
165
177
* @param array $options 当 $output=true,此选项有效
166
178
* $options = [
167
179
* 'type' => 'min' // 输出数据类型 min 压缩过的 raw 正常的
168
180
* 'file' => 'xx.json' // 输出文件路径;仅是文件名,则会取输入路径
169
181
* ]
170
182
*
171
- * @return string | bool
183
+ * @return string
172
184
*/
173
- public static function format ($ input , $ output = false , array $ options = [])
185
+ public static function format (string $ input , bool $ output = false , array $ options = []): string
174
186
{
175
- if (!is_string ($ input )) {
176
- return false ;
177
- }
178
-
179
- $ data = trim ($ input );
180
- if (file_exists ($ input )) {
181
- $ data = file_get_contents ($ input );
182
- }
183
-
184
- if (!$ data ) {
185
- return false ;
187
+ if (!$ data = self ::stripComments ($ input )) {
188
+ return '' ;
186
189
}
187
190
188
- $ data = preg_replace ([
189
- // 去掉所有多行注释/* .... */
190
- '/\/\*.*?\*\/\s*/is ' ,
191
- // 去掉所有单行注释//....
192
- '/\/\/.*?[\r\n]/is ' ,
193
- // 去掉空白行
194
- "/( \n[ \r])+/is "
195
- ], ['' , '' , "\n" ], $ data );
196
-
197
191
if (!$ output ) {
198
192
return $ data ;
199
193
}
@@ -240,4 +234,37 @@ public static function saveAs(string $data, string $output, array $options = [])
240
234
241
235
return file_put_contents ($ file , $ data );
242
236
}
237
+
238
+ /**
239
+ * @param string $json
240
+ *
241
+ * @return string
242
+ */
243
+ public static function clearComments (string $ json ): string
244
+ {
245
+ return self ::stripComments ($ json );
246
+ }
247
+
248
+ /**
249
+ * @param string $json
250
+ *
251
+ * @return string
252
+ */
253
+ public static function stripComments (string $ json ): string
254
+ {
255
+ if (!$ json = trim ($ json )) {
256
+ return '' ;
257
+ }
258
+
259
+ $ pattern = [
260
+ // 去掉所有多行注释/* .... */
261
+ '/\/\*.*?\*\/\s*/is ' ,
262
+ // 去掉所有单行注释//....
263
+ '/\/\/.*?[\r\n]/is ' ,
264
+ // 去掉空白行
265
+ "/( \n[ \r])+/is "
266
+ ];
267
+
268
+ return (string )preg_replace ($ pattern , ['' , '' , "\n" ], $ json );
269
+ }
243
270
}
0 commit comments