diff --git a/htdocs/core/lib/json.lib.php b/htdocs/core/lib/json.lib.php index f236e5ed304..1463f730c99 100644 --- a/htdocs/core/lib/json.lib.php +++ b/htdocs/core/lib/json.lib.php @@ -59,14 +59,21 @@ function dol_json_encode($elements) $num++; } } else { - $num = count($elements); + if (is_countable($elements)) { + $num = count($elements); + } } //var_dump($num); // determine type + if (is_numeric($elements)) { + return $elements; + } elseif (is_string($elements)) { + return '"'.$elements.'"'; + } if (is_numeric(key($elements)) && key($elements) == 0) { // indexed (list) - $keysofelements = array_keys($elements); // Elements array mus have key that does not start with 0 and end with num-1, so we will use this later. + $keysofelements = array_keys($elements); // Elements array must have key that does not start with 0 and end with num-1, so we will use this later. $output = '['; for ($i = 0, $last = ($num - 1); $i < $num; $i++) { if (!isset($elements[$keysofelements[$i]])) { @@ -253,6 +260,11 @@ function dol_json_decode($json, $assoc = false) $out = ''; $strLength = strlen($json); // Must stay strlen and not dol_strlen because we want technical length, not visible length + + if (is_numeric($json)) { + return $json; + } + for ($i = 0; $i < $strLength; $i++) { if (!$comment) { if ($i == 0 && !in_array($json[$i], array('{', '[', '"', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'))) { diff --git a/test/phpunit/JsonLibTest.php b/test/phpunit/JsonLibTest.php index f0168ff9787..113c13a804d 100644 --- a/test/phpunit/JsonLibTest.php +++ b/test/phpunit/JsonLibTest.php @@ -122,6 +122,16 @@ class JsonLibTest extends CommonClassTest $decoded = dol_json_decode($encoded, true); $this->assertEquals($arraytotest, $decoded, 'test for dol_json_xxx'); + $encoded = dol_json_encode(123); + $this->assertEquals(123, $encoded); + $decoded = dol_json_decode($encoded, true); + $this->assertEquals(123, $decoded, 'test for dol_json_xxx 123'); + + $encoded = dol_json_encode('abc'); + $this->assertEquals('"abc"', $encoded); + $decoded = dol_json_decode($encoded, true); + $this->assertEquals('abc', $decoded, "test for dol_json_xxx 'abc'"); + // Test with object $now = gmmktime(12, 0, 0, 1, 1, 1970); $objecttotest = new stdClass();