Bug #81532 [Opn->Csd]: Change of $depth behaviour in json_encode() on PHP 8.1

From: Date: Mon, 18 Oct 2021 10:42:19 +0000
Subject: Bug #81532 [Opn->Csd]: Change of $depth behaviour in json_encode() on PHP 8.1
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237260@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81532&edit=1

 ID:                 81532
 Updated by:         git@php.net
 Reported by:        benjamin dot morel at gmail dot com
 Summary:            Change of $depth behaviour in json_encode() on PHP
                     8.1
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            JSON related
 Operating System:   Linux
 PHP Version:        8.1.0RC4
 Block user comment: N
 Private report:     N

 New Comment:

Automatic comment on behalf of nikic
Revision: https://github.com/php/php-src/commit/6057cd2367003146285236f9c731405a8de990a3
Log: Fixed bug #81532


Previous Comments:
------------------------------------------------------------------------
[2021-10-18 06:48:49] benjamin dot morel at gmail dot com

Sorry, I mixed the expected and actual.
The expected result is false.

------------------------------------------------------------------------
[2021-10-17 12:43:55] benjamin dot morel at gmail dot com

Description:
------------
The behaviour of the $depth parameter of json_encode() has changed when encoding objects.

On PHP 8.1, all three examples below successfully encode the input object, while
on PHP <= 8.0, json_encode() returns false as this exceeds the max depth.

Discussion: https://externals.io/message/116279
Demo: https://3v4l.org/DoG4A

If we replace the $a object with an empty array [], it works: https://3v4l.org/vFgh0

Test script:
---------------
// depth 1
$a = new \stdClass();

// depth 2
$b = new \stdClass();
$b->x = $a;

// depth 3
$c = new \stdClass();
$c->x = [$a];

var_export(json_encode($a, 0, 0)); echo "\n";
var_export(json_encode($b, 0, 1)); echo "\n";
var_export(json_encode($c, 0, 2));

Expected result:
----------------
'{}'
'{"x":{}}'
'{"x":[{}]}'

Actual result:
--------------
false
false
false


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



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


Thread (3 messages)

« previous php.bugs (#237260) next »