2018-03-28 18:16:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Core;
|
|
|
|
|
|
|
|
use Icinga\Module\Director\Exception\JsonEncodeException;
|
|
|
|
|
|
|
|
class Json
|
|
|
|
{
|
2018-10-04 06:47:50 +02:00
|
|
|
public static function encode($mixed, $flags = null)
|
2018-03-28 18:16:30 +02:00
|
|
|
{
|
2019-04-04 13:11:39 +02:00
|
|
|
$result = \json_encode($mixed, $flags);
|
2018-03-28 18:16:30 +02:00
|
|
|
|
2018-10-04 06:47:50 +02:00
|
|
|
if ($result === false && json_last_error() !== JSON_ERROR_NONE) {
|
|
|
|
throw JsonEncodeException::forLastJsonError();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function decode($string)
|
|
|
|
{
|
2019-04-04 13:11:39 +02:00
|
|
|
$result = \json_decode($string);
|
2018-10-04 06:47:50 +02:00
|
|
|
|
|
|
|
if ($result === null && json_last_error() !== JSON_ERROR_NONE) {
|
2018-03-28 18:16:30 +02:00
|
|
|
throw JsonEncodeException::forLastJsonError();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|