This commit is contained in:
me
2026-07-23 15:29:13 +07:00
commit 9ec08a3e44
36 changed files with 2790 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
<?php
// json5
namespace Nightmare;
class Json
{
/**
* @param mixed ...$args
* @return string|false
*/
public static function encode(...$args) {
return json_encode(...$args);
}
/**
* @param string $data
* @param mixed ...$args
* @return mixed
*/
public static function decode($data, ...$args) {
$assoc = true;
if (count($args) > 0) {
$assoc = $args[0];
}
try {
return json5_decode($data, $assoc, ...array_slice($args, 1));
} catch(\Throwable $e) {
return null;
}
}
/**
* @param string $file
* @param mixed ...$args
* @return int|false
*/
public static function encode_file($file, ...$args) {
return file_put_contents($file, json_encode(...$args));
}
/**
* @param string $file
* @param mixed ...$args
* @return mixed
*/
public static function decode_file($file, ...$args) {
$assoc = true;
if (count($args) > 0) {
$assoc = $args[0];
}
return self::decode(file_get_contents($file), $assoc, ...array_slice($args, 1));
}
}