summaryrefslogtreecommitdiffstats
path: root/library/Director/Core/Json.php
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--library/Director/Core/Json.php34
1 files changed, 34 insertions, 0 deletions
diff --git a/library/Director/Core/Json.php b/library/Director/Core/Json.php
new file mode 100644
index 0000000..507349c
--- /dev/null
+++ b/library/Director/Core/Json.php
@@ -0,0 +1,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;
+ }
+}