Req #72073 [NoF->Opn]: Expose encoder_depth to jsonSerialize as parameter

From: Date: Sun, 22 Jan 2017 09:33:14 +0000
Subject: Req #72073 [NoF->Opn]: Expose encoder_depth to jsonSerialize as parameter
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-206848@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72073&edit=1 ID: 72073 Updated by: nikic@php.net Reported by: jani dot ollikainen at valve dot fi Summary: Expose encoder_depth to jsonSerialize as parameter -Status: No Feedback +Status: Open Type: Feature/Change Request Package: JSON related Operating System: All PHP Version: 5.5.34 Block user comment: N Private report: N New Comment: Discussion on internals was started. Previous Comments: ------------------------------------------------------------------------ [2017-01-22 04:22:48] 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-09 05:42:39] krakjoe@php.net Awaiting feedback on github. ------------------------------------------------------------------------ [2016-04-22 15:32:05] 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. ------------------------------------------------------------------------ [2016-04-22 08:24:09] jani dot ollikainen at valve dot fi Description: ------------ 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: --------------- <?php 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); var_dump($ret); $a=json_last_error(); var_dump($a); $b=json_last_error_msg(); var_dump($b); 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":[]}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}" int(0) string(8) "No error" Actual result: -------------- bool(false) int(1) string(28) "Maximum stack depth exceeded" ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72073&edit=1

« previous php.bugs (#206848) next »