php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81532 Change of $depth behaviour in json_encode() on PHP 8.1
Submitted: 2021-10-17 12:43 UTC Modified: -
From: benjamin dot morel at gmail dot com Assigned:
Status: Closed Package: JSON related
PHP Version: 8.1.0RC4 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: benjamin dot morel at gmail dot com
New email:
PHP Version: OS:

 

 [2021-10-17 12:43 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-10-18 06:48 UTC] benjamin dot morel at gmail dot com
Sorry, I mixed the expected and actual.
The expected result is false.
 [2021-10-18 10:42 UTC] git@php.net
Automatic comment on behalf of nikic
Revision: https://github.com/php/php-src/commit/6057cd2367003146285236f9c731405a8de990a3
Log: Fixed bug #81532
 [2021-10-18 10:42 UTC] git@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Dec 04 21:03:34 2021 UTC