Bug #81532 [NEW]: Change of $depth behaviour in json_encode() on PHP 8.1

From: Date: Sun, 17 Oct 2021 12:43:55 +0000
Subject: Bug #81532 [NEW]: Change of $depth behaviour in json_encode() on PHP 8.1
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237243@lists.php.net to get a copy of this message
From:             benjamin dot morel at gmail dot com
Operating system: Linux
PHP version:      8.1.0RC4
Package:          JSON related
Bug Type:         Bug
Bug description:Change of $depth behaviour in json_encode() on PHP 8.1

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 bug report at https://bugs.php.net/bug.php?id=81532&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=81532&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=81532&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=81532&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=81532&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=81532&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=81532&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=81532&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=81532&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=81532&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=81532&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81532&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=81532&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=81532&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=81532&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=81532&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=81532&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=81532&r=mysqlcfg


Thread (3 messages)

« previous php.bugs (#237243) next »