summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/json/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:23:16 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:23:16 +0000
commit3e97c51418e6d27e9a81906f347fcb7c78e27d4f (patch)
treeee596ce1bc9840661386f96f9b8d1f919a106317 /vendor/gipfl/json/src
parentInitial commit. (diff)
downloadicingaweb2-module-incubator-3e97c51418e6d27e9a81906f347fcb7c78e27d4f.tar.xz
icingaweb2-module-incubator-3e97c51418e6d27e9a81906f347fcb7c78e27d4f.zip
Adding upstream version 0.20.0.upstream/0.20.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gipfl/json/src')
-rw-r--r--vendor/gipfl/json/src/JsonDecodeException.php7
-rw-r--r--vendor/gipfl/json/src/JsonEncodeException.php7
-rw-r--r--vendor/gipfl/json/src/JsonException.php55
-rw-r--r--vendor/gipfl/json/src/JsonSerialization.php14
-rw-r--r--vendor/gipfl/json/src/JsonString.php68
-rw-r--r--vendor/gipfl/json/src/SerializationHelper.php55
6 files changed, 206 insertions, 0 deletions
diff --git a/vendor/gipfl/json/src/JsonDecodeException.php b/vendor/gipfl/json/src/JsonDecodeException.php
new file mode 100644
index 0000000..cd19aa7
--- /dev/null
+++ b/vendor/gipfl/json/src/JsonDecodeException.php
@@ -0,0 +1,7 @@
+<?php
+
+namespace gipfl\Json;
+
+class JsonDecodeException extends JsonException
+{
+}
diff --git a/vendor/gipfl/json/src/JsonEncodeException.php b/vendor/gipfl/json/src/JsonEncodeException.php
new file mode 100644
index 0000000..e9fcc5f
--- /dev/null
+++ b/vendor/gipfl/json/src/JsonEncodeException.php
@@ -0,0 +1,7 @@
+<?php
+
+namespace gipfl\Json;
+
+class JsonEncodeException extends JsonException
+{
+}
diff --git a/vendor/gipfl/json/src/JsonException.php b/vendor/gipfl/json/src/JsonException.php
new file mode 100644
index 0000000..e7b5f36
--- /dev/null
+++ b/vendor/gipfl/json/src/JsonException.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace gipfl\Json;
+
+use Exception;
+
+class JsonException extends Exception
+{
+ public static function forLastJsonError($msg = null)
+ {
+ if ($msg === null) {
+ return new static(static::getJsonErrorMessage(\json_last_error()));
+ } else {
+ return new static($msg . ': ' . static::getJsonErrorMessage(\json_last_error()));
+ }
+ }
+
+ public static function getJsonErrorMessage($code)
+ {
+ $map = [
+ JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
+ JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
+ JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
+ JSON_ERROR_SYNTAX => 'JSON Syntax error',
+ JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
+ ];
+ if (\array_key_exists($code, $map)) {
+ return $map[$code];
+ }
+
+ if (PHP_VERSION_ID >= 50500) {
+ $map = [
+ JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded',
+ JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded',
+ JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given',
+ ];
+ if (\array_key_exists($code, $map)) {
+ return $map[$code];
+ }
+ }
+
+ if (PHP_VERSION_ID >= 70000) {
+ $map = [
+ JSON_ERROR_INVALID_PROPERTY_NAME => 'A property name that cannot be encoded was given',
+ JSON_ERROR_UTF16 => 'Malformed UTF-16 characters, possibly incorrectly encoded',
+ ];
+
+ if (\array_key_exists($code, $map)) {
+ return $map[$code];
+ }
+ }
+
+ return 'An error occured when parsing a JSON string';
+ }
+}
diff --git a/vendor/gipfl/json/src/JsonSerialization.php b/vendor/gipfl/json/src/JsonSerialization.php
new file mode 100644
index 0000000..f5b058e
--- /dev/null
+++ b/vendor/gipfl/json/src/JsonSerialization.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace gipfl\Json;
+
+use JsonSerializable;
+
+interface JsonSerialization extends JsonSerializable
+{
+ /**
+ * @param mixed $any
+ * @return static
+ */
+ public static function fromSerialization($any);
+}
diff --git a/vendor/gipfl/json/src/JsonString.php b/vendor/gipfl/json/src/JsonString.php
new file mode 100644
index 0000000..b9e22b6
--- /dev/null
+++ b/vendor/gipfl/json/src/JsonString.php
@@ -0,0 +1,68 @@
+<?php
+
+namespace gipfl\Json;
+
+use function json_decode;
+use function json_encode;
+use function json_last_error;
+
+class JsonString
+{
+ const DEFAULT_FLAGS = JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
+
+ /**
+ * Encode with well-known flags, as we require the result to be reproducible
+ *
+ * @param $mixed
+ * @param int|null $flags
+ * @return string
+ * @throws JsonEncodeException
+ */
+ public static function encode($mixed, $flags = null)
+ {
+ if ($flags === null) {
+ $flags = self::DEFAULT_FLAGS;
+ } else {
+ $flags = self::DEFAULT_FLAGS | $flags;
+ }
+ $result = json_encode($mixed, $flags);
+
+ if ($result === false && json_last_error() !== JSON_ERROR_NONE) {
+ throw JsonEncodeException::forLastJsonError();
+ }
+
+ return $result;
+ }
+
+ /**
+ * Decode the given JSON string and make sure we get a meaningful Exception
+ *
+ * @param string $string
+ * @return mixed
+ * @throws JsonDecodeException
+ */
+ public static function decode($string)
+ {
+ $result = json_decode($string);
+
+ if ($result === null && json_last_error() !== JSON_ERROR_NONE) {
+ throw JsonDecodeException::forLastJsonError();
+ }
+
+ return $result;
+ }
+
+ /**
+ * @param $string
+ * @return ?string
+ * @throws JsonDecodeException
+ */
+ public static function decodeOptional($string)
+ {
+ if ($string === null) {
+ return null;
+ }
+
+ return static::decode($string);
+ }
+}
diff --git a/vendor/gipfl/json/src/SerializationHelper.php b/vendor/gipfl/json/src/SerializationHelper.php
new file mode 100644
index 0000000..0714e30
--- /dev/null
+++ b/vendor/gipfl/json/src/SerializationHelper.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace gipfl\Json;
+
+use InvalidArgumentException;
+use JsonSerializable;
+use stdClass;
+
+class SerializationHelper
+{
+ /**
+ * TODO: Check whether json_encode() is faster
+ *
+ * @param mixed $value
+ * @return bool
+ */
+ public static function assertSerializableValue($value)
+ {
+ if ($value === null || is_scalar($value)) {
+ return true;
+ }
+ if (is_object($value)) {
+ if ($value instanceof JsonSerializable) {
+ return true;
+ }
+
+ if ($value instanceof stdClass) {
+ foreach ((array) $value as $val) {
+ static::assertSerializableValue($val);
+ }
+
+ return true;
+ }
+ }
+
+ if (is_array($value)) {
+ foreach ($value as $val) {
+ static::assertSerializableValue($val);
+ }
+
+ return true;
+ }
+
+ throw new InvalidArgumentException('Serializable value expected, got ' . static::getPhpType($value));
+ }
+
+ public static function getPhpType($var)
+ {
+ if (is_object($var)) {
+ return get_class($var);
+ }
+
+ return gettype($var);
+ }
+}