Bug #81685 [Opn->Nab]: Using json_decode with an integer as first parameter doesn't return NULL
Edit report at https://bugs.php.net/bug.php?id=81685&edit=1
ID: 81685
Updated by: cmb@php.net
Reported by: joaquimsb89 at gmail dot com
Summary: Using json_decode with an integer as first parameter
doesn't return NULL
-Status: Open
+Status: Not a bug
Type: Bug
Package: JSON related
Operating System: Debian & Arch
PHP Version: 7.4.26
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
See <https://datatracker.ietf.org/doc/html/rfc8259#section-2>:
| A JSON text is a serialized value. Note that certain previous
| specifications of JSON constrained a JSON text to be an object or
| an array.
So the actual behavior of json_decode() looks good to me.
Previous Comments:
------------------------------------------------------------------------
[2021-12-01 14:40:38] joaquimsb89 at gmail dot com
Description:
------------
When using the json_decode() function and passing an integer as the first parameter, or with the
strval() function, the return value is the integer itself instead of an expected NULL, expecting
"Syntax error" as the error description.
Behaviour observed in the following environments:
PHP 7.4.26, debian 10/buster, installed via deb sury repo
PHP 8.0.13, Arch Linux, installed via pacman -S php
PHP 8.1.0, debian 11/bullseye, installed via deb sury repo
Test script:
---------------
<?php
var_dump(json_decode('', true));
var_dump(json_last_error_msg());
echo "\n";
var_dump(json_decode(123, true));
var_dump(json_last_error_msg());
echo "\n";
var_dump(json_decode(strval(123), true));
var_dump(json_last_error_msg());
echo "\n";
var_dump(json_decode('{"a":123}', true));
var_dump(json_last_error_msg());
Expected result:
----------------
NULL
string(12) "Syntax error"
NULL
string(12) "Syntax error"
NULL
string(12) "Syntax error"
array(1) {
["a"]=>
int(123)
}
string(8) "No error"
Actual result:
--------------
NULL
string(12) "Syntax error"
int(123)
string(8) "No error"
int(123)
string(8) "No error"
array(1) {
["a"]=>
int(123)
}
string(8) "No error"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81685&edit=1
Thread (5 messages)