@@ 28-37 (lines=10) @@ | ||
25 | * @throws \Gnumoksha\FreeIpa\Infra\Json\JsonException |
|
26 | * @see \json_encode() |
|
27 | */ |
|
28 | public static function encode($value, int $options = 0, int $depth = 512): string |
|
29 | { |
|
30 | $encoded = json_encode($value, $options, $depth); |
|
31 | ||
32 | if (json_last_error() !== JSON_ERROR_NONE) { |
|
33 | throw new JsonException(sprintf('Unable to encode json. Error was: "%s".', json_last_error_msg())); |
|
34 | } |
|
35 | ||
36 | return (string)$encoded; |
|
37 | } |
|
38 | ||
39 | /** |
|
40 | * Decode a value from json. |
|
@@ 46-55 (lines=10) @@ | ||
43 | * @throws \Gnumoksha\FreeIpa\Infra\Json\JsonException |
|
44 | * @see \json_decode() |
|
45 | */ |
|
46 | public static function decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0) |
|
47 | { |
|
48 | $decoded = json_decode($json, $assoc, $depth, $options); |
|
49 | ||
50 | if (json_last_error() !== JSON_ERROR_NONE) { |
|
51 | throw new JsonException(sprintf('Unable to decode json. Error was: "%s".', json_last_error_msg())); |
|
52 | } |
|
53 | ||
54 | return $decoded; |
|
55 | } |
|
56 | } |
|
57 |