|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #72073 Expose encoder_depth to jsonSerialize as parameter
Submitted: 2016-04-22 08:24 UTC Modified: 2017-04-30 15:20 UTC
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: jani dot ollikainen at valve dot fi Assigned: bukka (profile)
Status: Wont fix Package: JSON related
PHP Version: 5.5.34 OS: All
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2016-04-22 08:24 UTC] jani dot ollikainen at valve dot fi
We have objects that do dynamic loading and there might be recursions. We could use recursion depth to decide when we want to do dynamic loading and when not.

Now there doesn't seem to be any way to know how deep is the json_encode process going. If we try to use some level in jsonSerialize it just calls one of those, no recursion there.

This could be easily archived by exposing the internal encoder_depth to jsonSerialize.

Test script:
class O implements \JsonSerializable {
        public function jsonSerialize($depth = 0) {
                if ($depth >= 100)
                        return array();
                return array('i' => $this->i, 'o' => $this->o);
$first = new O;
$first->i = 0; $last = $first;
for ($i = 1; $i < 500; $i++) {
        $new = New O;
        $new->i = $i; $new->o = null; $last->o = $new; $last = $new;
$ret = json_encode($first, 0, 256);

Expected result:
string(1292) "{"i":0,"o":{"i":1,"o":{"i":2,"o":{"i":3,"o":{"i":4,"o":{"i":5,"o":{"i":6,"o":{"i":7,"o":{"i":8,"o":{"i":9,"o":{"i":10,"o":{"i":11,"o":{"i":12,"o":{"i":13,"o":{"i":14,"o":{"i":15,"o":{"i":16,"o":{"i":17,"o":{"i":18,"o":{"i":19,"o":{"i":20,"o":{"i":21,"o":{"i":22,"o":{"i":23,"o":{"i":24,"o":{"i":25,"o":{"i":26,"o":{"i":27,"o":{"i":28,"o":{"i":29,"o":{"i":30,"o":{"i":31,"o":{"i":32,"o":{"i":33,"o":{"i":34,"o":{"i":35,"o":{"i":36,"o":{"i":37,"o":{"i":38,"o":{"i":39,"o":{"i":40,"o":{"i":41,"o":{"i":42,"o":{"i":43,"o":{"i":44,"o":{"i":45,"o":{"i":46,"o":{"i":47,"o":{"i":48,"o":{"i":49,"o":{"i":50,"o":{"i":51,"o":{"i":52,"o":{"i":53,"o":{"i":54,"o":{"i":55,"o":{"i":56,"o":{"i":57,"o":{"i":58,"o":{"i":59,"o":{"i":60,"o":{"i":61,"o":{"i":62,"o":{"i":63,"o":{"i":64,"o":{"i":65,"o":{"i":66,"o":{"i":67,"o":{"i":68,"o":{"i":69,"o":{"i":70,"o":{"i":71,"o":{"i":72,"o":{"i":73,"o":{"i":74,"o":{"i":75,"o":{"i":76,"o":{"i":77,"o":{"i":78,"o":{"i":79,"o":{"i":80,"o":{"i":81,"o":{"i":82,"o":{"i":83,"o":{"i":84,"o":{"i":85,"o":{"i":86,"o":{"i":87,"o":{"i":88,"o":{"i":89,"o":{"i":90,"o":{"i":91,"o":{"i":92,"o":{"i":93,"o":{"i":94,"o":{"i":95,"o":{"i":96,"o":{"i":97,"o":{"i":98,"o":{"i":99,"o":[]}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}"
string(8) "No error"

Actual result:
string(28) "Maximum stack depth exceeded"


jsonserializedepth (last revision 2016-04-22 17:11 UTC by jani dot ollikainen at valve dot fi)
jsonserializedepth-55 (last revision 2016-04-22 15:09 UTC by jani dot ollikainen at valve dot fi)

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-04-22 15:32 UTC] jani dot ollikainen at valve dot fi
About the patches: jsonserializedepth is made to PHP7, and jsonserializedepth-55 for PHP5.5. I don't know to which version you would consider including it and if there are differences in versions between, but the patches idea should be pretty obvious.
 [2017-01-09 05:42 UTC]
-Status: Open +Status: Feedback
 [2017-01-09 05:42 UTC]
Awaiting feedback on github.
 [2017-01-22 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2017-01-22 09:33 UTC]
-Status: No Feedback +Status: Open
 [2017-01-22 09:33 UTC]
Discussion on internals was started.
 [2017-04-30 15:20 UTC]
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: bukka
 [2017-04-30 15:20 UTC]
As discussed, there is a BC concern so this is not going happen unless there is an approved RFC.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jul 16 19:01:28 2024 UTC