On Wed, Jan 11, 2017 at 8:06 AM, Jani Ollikainen <jani.ollikai...@valve.fi> wrote:
> Hi, > > I've made a pull request where I was asked to start internal discussion > about it.. The pull request can be found: > https://github.com/php/php-src/pull/1884 > Which relates to request I've also made: > https://bugs.php.net/bug.php?id=72073 > > For short the thing is: > "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." > > As I don't know any other way to get the depth info from jsonSerialize, as > it isn't called recursively in PHP side, so adding some static class > variable and incrementing/decrementing it in jsonSerialize will just not > work. > > Then to why would someone really need that? Well, we have models that uses > MongoDB as a database (created from MongoDB objects) and the models can > have references to other models (coming from MongoDB objects). That loading > will happen dynamically and there might be recursions, but we want > still to serialize them as we really don't need to go that deep even if > there is a recursion. This way we can control the depth we are loading new > objects and > can avoid recursion happening in jsonSerialize and can get the data we > need serialized. > > If you are thinking why don't you just use the depth option of > json_encode, well unfortunately it doesn't help. It seems to go too deep > and get recursion errors and would just later limit the depth it's going as > my example will show if you try if you run it without patched json. > > Any comments/ideas? > > I don't really like this. The reason is that you don't actually modify JsonSerializable interface for the obvious BC break that it would cause it. It means that the function just gets this parameter and it kind of raises a question how we should document it? The solution would be to extend JsonSerializable with some new interface. However I don't think it's worth it for such thing. Maybe you should consider to pre-process your data before passing it to json_encode... Cheers Jakub