Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97765 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 37629 invoked from network); 15 Jan 2017 20:49:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Jan 2017 20:49:16 -0000 Authentication-Results: pb1.pair.com header.from=jakub.php@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=jakub.php@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.161.182 as permitted sender) X-PHP-List-Original-Sender: jakub.php@gmail.com X-Host-Fingerprint: 209.85.161.182 mail-yw0-f182.google.com Received: from [209.85.161.182] ([209.85.161.182:36791] helo=mail-yw0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AB/8F-00729-840EB785 for ; Sun, 15 Jan 2017 15:49:12 -0500 Received: by mail-yw0-f182.google.com with SMTP id a10so58639801ywa.3 for ; Sun, 15 Jan 2017 12:49:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=fw7ic6GqgaodxMpPbDySL/LLek50auXS/o3GXSN6q1o=; b=Pl4yifyW68BxP0NaCyWuXlXBCr9m33bX9N8PZZ23Yld0oFfnmaYOMCD/M4x0tuuzat IeS5w9VmfzEimw+XouWVH5333zEYEMsa46qVJDxVNlVx+BBSKRoZXlEthDT/57qvmG5F xxJ87qaPvFArz32gpSJIPStNVN+3wYus+Mv5CZVW2NuczcCpRUKbnuYUKuIdcG4+YkBf pMuq0ihYkV/nmzZTChw/yy5VACvVx6gt1RyfTlCwd97u0XszYxshOdWK7dt+W1cNp0bZ 50jd+w/Z9sr7Z6aVpwQ+rsEPM9yIkNexYzMFl5AH6S11+KtHKnDv12emf2D/FiLnq6Ub hobA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=fw7ic6GqgaodxMpPbDySL/LLek50auXS/o3GXSN6q1o=; b=GgkVxRjl4NcgOGXLk1IEOyX8URXyMuFFz34bTJkMY9vu6/hdGxQAmtW/I8CMo98JUK YniOi1UySHtnOHhiSZuk4fAwiNLgENKBzNa6eSn38NsJiz+321oml3AVcIBm4Yag8fqf xOK3fyeezH1mT9rz4gtR3giaE5f1v7frZrKc56NcMJF13NNHyaiupYHqCaOwUYqyEsFw pjiIfLM6UnJ3BCOAjxhFt8K57GPBMoGufzrUTO7Q+rLHxx2hkO/mDLE+RDXFxRgAsV+y T6QHcNX+ZCK80qGE8v+3fncXECevAHi3JdlZNHxFoCWDuoaOCAeBrE6apAgg1JtdvVw3 QAHQ== X-Gm-Message-State: AIkVDXKUjL2iboHu3A20kEElZdUkb8+EXV81InzA22oSDrJEJ8sxWnwYMkNJYyOfvcQcOyojJCEy1Qbrd8Hesw== X-Received: by 10.129.48.77 with SMTP id w74mr21416207yww.99.1484513348987; Sun, 15 Jan 2017 12:49:08 -0800 (PST) MIME-Version: 1.0 Sender: jakub.php@gmail.com Received: by 10.129.72.73 with HTTP; Sun, 15 Jan 2017 12:49:08 -0800 (PST) In-Reply-To: References: Date: Sun, 15 Jan 2017 20:49:08 +0000 X-Google-Sender-Auth: 0vKNufcWQvbb9SxbovQcqy3xVrw Message-ID: To: Jani Ollikainen Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a114084a2d4afd205462832f9 Subject: Re: [PHP-DEV] Exposing jsonSerialize's depth From: bukka@php.net (Jakub Zelenka) --001a114084a2d4afd205462832f9 Content-Type: text/plain; charset=UTF-8 On Wed, Jan 11, 2017 at 8:06 AM, Jani Ollikainen 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 --001a114084a2d4afd205462832f9--