From 4e327ed54d90fd52f4b82d82979cc79b2f7899f2 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 4 Oct 2018 06:47:50 +0200 Subject: [PATCH] Json: allow null/false --- library/Director/Core/Json.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/library/Director/Core/Json.php b/library/Director/Core/Json.php index 5315d978..fc3288c6 100644 --- a/library/Director/Core/Json.php +++ b/library/Director/Core/Json.php @@ -6,11 +6,22 @@ use Icinga\Module\Director\Exception\JsonEncodeException; 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(); }