On Wed, Apr 29, 2020 at 8:11 AM Nikita Popov <nikita....@gmail.com> wrote:
> > Doing this would also make some extensions more convenient to use (e.g. > > memcached with the json serializer, using json encoding for uses such as > > error messages in miscellaneous extensions, etc.) > > > > Another advantage would be that the JsonSerializable interface would be > always available. This would make things simpler for extensions that want > to hook into that. Currently we have a bunch of classes like DateTime, > which do have custom JSON serialization behavior, but do not implement > JsonSerializable, because the class is not always available. > > Technically, we could have these classes always implement the method and only attach the interface conditionally during MINIT, but for sure the mechanics become both simpler and more reliable for userspace scripts. This gets a pretty easy +1 from me. Looking at Ubuntu 20.04, they seem to have json built-in to the core package anyway. I'm not sure about other distros. > > P.S. What are your thoughts about adding additional conversion specifiers > > such as %j or %v to PHP to call JSON with the default options. > > It's a feature similar to those I've seen in programming languages such > as > > golang - https://golang.org/pkg/fmt/#hdr-Printing > > > > - `printf("console.log("value from php", %j);\n", $value)` > > - `printf("Some command returned %j\n", $boolValue)` > > > > Uh, dunno. Is it really common to want JSON inside printf? I see printf > mostly as something used to output to console, not so much in a web / data > interchange context. > > It's possible (with some elbow-grease to make the %j approach a bit more efficient since one could stream the serializer straight to the output, but if that's really desired, there are other more explicit ways to make it happen. Ignoring streaming efficiency, the only real gain is replacing `"%s", json_encode($x)` with `"%j", $x` which is of questionable gain. In terms of getting it passed, I'd focus on just the first part, making JSON a "core" builtin. Let the printf modifier be a separate RFC, or at least a separate question. -Sara