1: <?php
2:
3: // json5
4:
5: namespace nightmare;
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:
28: try {
29: return json5_decode($data, $assoc, ...array_slice($args, 1));
30: } catch(\Throwable $e) {
31: return null;
32: }
33: }
34:
35: /**
36: * @param string $file
37: * @param mixed ...$args
38: * @return int|false
39: */
40: public static function encode_file($file, ...$args) {
41: return file_put_contents($file, json_encode(...$args));
42: }
43:
44: /**
45: * @param string $file
46: * @param mixed ...$args
47: * @return mixed
48: */
49: public static function decode_file($file, ...$args) {
50: $assoc = true;
51: if (count($args) > 0) {
52: $assoc = $args[0];
53: }
54: return self::decode(file_get_contents($file), $assoc, ...array_slice($args, 1));
55: }
56: }
57: