Doc #81685 [Com]: Using json_decode with an integer as first parameter doesn't return NULL

From: Date: Thu, 30 Dec 2021 10:40:07 +0000
Subject: Doc #81685 [Com]: Using json_decode with an integer as first parameter doesn't return NULL
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-19377@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81685&edit=1

 ID:                 81685
 Comment by:         a at b dot c dot de
 Reported by:        joaquimsb89 at gmail dot com
 Summary:            Using json_decode with an integer as first parameter
                     doesn't return NULL
 Status:             Open
 Type:               Documentation Problem
 Package:            JSON related
 Operating System:   Debian & Arch
 PHP Version:        7.4.26
 Block user comment: N
 Private report:     N

 New Comment:

Isn't this just bog-standard type juggling? Passed an integer argument to a string parameter,
it recast the integer as a string?


declare(strict_types=1);

$v = json_decode(123, true);
var_dump($v);
var_dump(json_last_error_msg());


Previous Comments:
------------------------------------------------------------------------
[2021-12-01 16:16:21] me at davidgarcia dot cat

I am not aiming to start an argument, so I won't post more messages. But I believe some
clarification might help. Thanks for your understanding,

----

Won't this behaviour cause, then, a potential error when processing external requests (like API
calls) as we won't be able to retrieve a "valid JSON object" that can be converted to
an object via json_decode($payload, false) or to an array via json_code($payload, true)?

Even the official PHP website does not have an example for any input payload that is not a
JSON-parsed string format, including {} and / or [] and bearing in mind the
"key":"value" format.

https://www.php.net/json_decode

If the RFC 8259 is implemented as expected, then it looks like there's a lack of documentation
on the PHP website (that leads to confusion) to cover these cases.

Also, the PHP website points that the implemented logic is for RFC 7159 - which is being adjusted by
the RFC 8259, so it's unfair point to "another RFC" than the one mentioned on the
website.

Extra reading would be appreciated here. Would be possible getting directions on where other
examples can be found, please, so it's possible to contrast this specific case?

------------------------------------------------------------------------
[2021-12-01 15:46:44] cmb@php.net

RFC 8259 is a standards document; neither that Wikipedia page nor
that json.org page are.  And besides, adhering Postel's law is
almost never a bad idea.

------------------------------------------------------------------------
[2021-12-01 15:41:59] me at davidgarcia dot cat

On the same web link that was shared previously:

https://datatracker.ietf.org/doc/html/rfc8259#section-2

A JSON text is a sequence of tokens.  The set of tokens includes six
structural characters, strings, numbers, and three literal names.

----

Wikipedia definition

https://en.wikipedia.org/wiki/JSON

JSON [...] is an open standard file format and data interchange format that uses human-readable text
to store and transmit data objects consisting of attribute–value pairs and arrays (or other
serializable values) [...]

----

JSON

https://www.json.org/json-en.html

(similar description and detailed chart to explain in detail)

====

To me, passing an integer or a non-formatted JSON string means there's a bug here...

------------------------------------------------------------------------
[2021-12-01 15:18:57] cmb@php.net

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.

------------------------------------------------------------------------
[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)

« previous php.doc.bugs (#19377) next »