Req #72073 [NEW]: Expose encoder_depth to jsonSerialize as parameter

From: Date: Fri, 22 Apr 2016 08:24:15 +0000
Subject: Req #72073 [NEW]: Expose encoder_depth to jsonSerialize as parameter
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-200684@lists.php.net to get a copy of this message
From:             jani dot ollikainen at valve dot fi
Operating system: All
PHP version:      5.5.34
Package:          JSON related
Bug Type:         Feature/Change Request
Bug description:Expose encoder_depth to jsonSerialize as parameter

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



Thread (6 messages)

« previous php.bugs (#200684) next »