Bug #69187 [Com]: json_last_error return BC in PHP7

From: Date: Fri, 06 Mar 2015 11:37:25 +0000
Subject: Bug #69187 [Com]: json_last_error return BC in PHP7
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-191209@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69187&edit=1

 ID:                 69187
 Comment by:         hdtfonseca at gmail dot com
 Reported by:        hdtfonseca at gmail dot com
 Summary:            json_last_error return BC in PHP7
 Status:             Assigned
 Type:               Bug
 Package:            JSON related
 Operating System:   linux
 PHP Version:        master-Git-2015-03-04 (Git)
 Assigned To:        bukka
 Block user comment: N
 Private report:     N

 New Comment:

I think @nikic has a point. What about, TRUE, 1 and 0 values? Shouldn't they all output
JSON_ERROR_SYNTAX?

In PHP 5.6 they all return JSON_ERROR_NONE but I think that 1, 0, "", TRUE, FALSE and NULL
should be consistent. At least "", FALSE and 0 should.


Previous Comments:
------------------------------------------------------------------------
[2015-03-06 07:23:08] nikic@php.net

@bukka I think the current PHP 7 behavior is correct. The json_decode function accepts a string and
is documented to accept a string. According to our coercion rules null and false correspond to the
empty string "", which is rightly no longer valid.

As such I don't think anything needs to change here. json_decode does not give you any
guarantees that json_decode($nonString) === $nonString and I don't see why we'd want to
introduce such a behavior.

------------------------------------------------------------------------
[2015-03-05 19:23:14] aharvey@php.net

Well, that was embarrassing. :)

Thanks for looking into this and in advance for the patch!

------------------------------------------------------------------------
[2015-03-05 18:23:46] bukka@php.net

Hi Adam, this is actually caused by your commit ( https://github.com/php/php-src/commit/a7b3abe4e6f5e2fdfd8d55b676c9ca6b3f9c8cc8
) :). The reason is the null is converted to empty string... The same will be for false.

I think that it makes sense to not emit error for null or false but only for empty string.
json_decode already returns number when you pass number (json_decode(1)) so it makes sense to have
it consistent. There is still a small inconsistency with boolean - json_decode(false) returns NULL
and json_decode(true) returns 1. However I'm not sure if it's worthy it to break BC (even
slightly)...

I will apply the patch for NULL and FALSE in a couple days if no objections.

------------------------------------------------------------------------
[2015-03-04 19:36:46] aharvey@php.net

Jakub, is this expected, and if so, can you update UPGRADING with the situations where this will
happen, please?

------------------------------------------------------------------------
[2015-03-04 18:13:44] hdtfonseca at gmail dot com

Description:
------------
The output of the function json_last_error() is different in PHP master in some situations and this
is causing a BC break.



Test script:
---------------
json_decode(null, true); 
echo json_last_error();

Expected result:
----------------
0

Actual result:
--------------
4


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=69187&edit=1


Thread (13 messages)

« previous php.bugs (#191209) next »