Json: allow null/false

This commit is contained in:
Thomas Gelf 2018-10-04 06:47:50 +02:00
parent 0b341c6db1
commit 4e327ed54d
1 changed files with 14 additions and 3 deletions

View File

@ -6,11 +6,22 @@ use Icinga\Module\Director\Exception\JsonEncodeException;
class Json class Json
{ {
public static function encode($string) public static function encode($mixed, $flags = null)
{ {
$result = json_encode($string); $result = json_encode($mixed, $flags);
if ($result === false) { if ($result === false && json_last_error() !== JSON_ERROR_NONE) {
throw JsonEncodeException::forLastJsonError();
}
return $result;
}
public static function decode($string)
{
$result = json_decode($string);
if ($result === null && json_last_error() !== JSON_ERROR_NONE) {
throw JsonEncodeException::forLastJsonError(); throw JsonEncodeException::forLastJsonError();
} }