Fix warnings

This commit is contained in:
Laurent Destailleur 2024-06-01 15:30:43 +02:00
parent 8d16c2e996
commit 580a021c7d
2 changed files with 24 additions and 2 deletions

View file

@ -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'))) {

View file

@ -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();