summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Schneider2025-03-25 03:15:58 +0100
committerChristian Schneider2025-03-25 03:15:58 +0100
commit470fef0c3b772bf24ed161641ae9d2bd92fac4fa (patch)
tree33cec41d07fb7e7bbbbc8988e498cc740f2f1ce5
parentd713c9103aaeffdcf47f5ce73e2c75dac4f1b76b (diff)
downloaditools-470fef0c3b772bf24ed161641ae9d2bd92fac4fa.tar.gz
itools-470fef0c3b772bf24ed161641ae9d2bd92fac4fa.tar.bz2
itools-470fef0c3b772bf24ed161641ae9d2bd92fac4fa.zip
Handle pretty printed null including newlines in it::json_decode
-rw-r--r--it.class2
-rwxr-xr-xtest/it.t1
2 files changed, 2 insertions, 1 deletions
diff --git a/it.class b/it.class
index 0733819..b93d49a 100644
--- a/it.class
+++ b/it.class
@@ -1263,7 +1263,7 @@ static function json_encode($data, $p = [])
*/
static function json_decode($json, $p = [])
{
- return ($data = json_decode($json, $p['assoc'])) === null && $json != 'null' ? it::error((array)$p['it_error'] + ['title' => "invalid json", 'body' => $json]) : $data;
+ return ($data = json_decode($json, $p['assoc'])) === null && trim($json) != 'null' ? it::error((array)$p['it_error'] + ['title' => "invalid json", 'body' => $json]) : $data;
}
/**
diff --git a/test/it.t b/test/it.t
index cee4fa6..535f185 100755
--- a/test/it.t
+++ b/test/it.t
@@ -481,6 +481,7 @@ foreach ([$dummy, false, true, null, 1, "a", "Ä", "/", []] as $var)
is(it::json_decode(it::json_encode($var)), $var);
is(it::json_decode('{"foo":"bar"}')->foo, "bar");
is(it::json_decode('{"foo":"bar"}', ['assoc' => true])['foo'], "bar");
+is(it::json_decode("\nnull\n\n", ['it_error' => ['fatal' => true]]), null, 'json_decode pretty printed null value');
is(it::cdist(null), true);
is(it::cdist(), true);