Given a float number, after a json encoding and a subsequent decoding, the variable type float (or double) sometimes it's not preserved.
$n1 = 1.23;
$json = json_encode($n1); // '1.23'
$n2 = json_decode($json);
$t1 = gettype($n1); // 'double'
$t2 = gettype($n2); // 'double'
$d = $n1 === $n2; // true
However, when decimals are '0' the result is different:
$n1 = 1.0;
$json = json_encode($n1); // '1'
$n2 = json_decode($json);
$t1 = gettype($n1); // 'double'
$t2 = gettype($n2); // 'integer'
$d = $n1 === $n2; // false
I have to conclude that the behaviour of the json encode / decode functions is based on data value, therefore hard to predict, and ultimately quite inconsistent.
Is there any way to ensure to preserve the type of the variable during the json encode / decode process?