1: <?php
2:
3: // json5
4:
5: namespace ngatngay;
6:
7: class json
8: {
9: /**
10: * @param mixed ...$args
11: * @return string|false
12: */
13: public static function encode(...$args) {
14: return json_encode(...$args);
15: }
16:
17: /**
18: * @param string $data
19: * @param mixed ...$args
20: * @return mixed
21: */
22: public static function decode($data, ...$args) {
23: $assoc = true;
24: if (count($args) > 0) {
25: $assoc = $args[0];
26: }
27: return json5_decode($data, $assoc, ...array_slice($args, 1));
28: }
29:
30: /**
31: * @param string $file
32: * @param mixed ...$args
33: * @return int|false
34: */
35: public static function encode_file($file, ...$args) {
36: return file_put_contents($file, json_encode(...$args));
37: }
38:
39: /**
40: * @param string $file
41: * @param mixed ...$args
42: * @return mixed
43: */
44: public static function decode_file($file, ...$args) {
45: $assoc = true;
46: if (count($args) > 0) {
47: $assoc = $args[0];
48: }
49: return json5_decode(file_get_contents($file), $assoc, ...array_slice($args, 1));
50: }
51: }
52: