summaryrefslogtreecommitdiffstats
path: root/library/Director/Core/Json.php
blob: 507349cfd75f17ef45d4c186f6332f3edd1b3cd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php

namespace Icinga\Module\Director\Core;

use Icinga\Module\Director\Exception\JsonEncodeException;

class Json
{
    public static function encode($mixed, $flags = null)
    {
        if ($flags === null) {
            $result = \json_encode($mixed);
        } else {
            $result = \json_encode($mixed, $flags);
        }

        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();
        }

        return $result;
    }
}