update
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user