Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:78723 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 43592 invoked from network); 5 Nov 2014 14:33:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Nov 2014 14:33:29 -0000 Authentication-Results: pb1.pair.com header.from=are.you.winning@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=are.you.winning@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.192.52 as permitted sender) X-PHP-List-Original-Sender: are.you.winning@gmail.com X-Host-Fingerprint: 209.85.192.52 mail-qg0-f52.google.com Received: from [209.85.192.52] ([209.85.192.52:33786] helo=mail-qg0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 62/C0-39230-7353A545 for ; Wed, 05 Nov 2014 09:33:28 -0500 Received: by mail-qg0-f52.google.com with SMTP id a108so11950519qge.25 for ; Wed, 05 Nov 2014 06:33:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=vHA+l+jietLBEi9oYzSLV8v2VKP9QaKZGRojsBdgPKQ=; b=QmuNlEH4m9oWXTU93m1XeclAe3b5Fsb7g6dNrD9Dwo73ju3kF8B+KaI9f8UC+74EEk 1IUy9/CWMCo/nAjGxMzTaeFnoi5SeUYI3LjZaGYStg20/Zh0Kb6biklwJWL1SKe13r0H Wnj8uKgysKRKzrNPhhV0jZmoC5iPuFyWVkreFk0M+2doNibzzhuMO22C6LjqmjT6J0Ag B/ZqolMtKTrEF5mn/ssGmBnEIC3FNoeYCJ5uYzst5B2tO2inOgCH62P4bJE/vsjBRlcA l9GCnWkY5I20CXG21z1CejgIyyLcPjEZLxiI56f/lFiu+2Z9SHqkJAArx70mlIgRwb9G QAdw== MIME-Version: 1.0 X-Received: by 10.224.132.7 with SMTP id z7mr44788323qas.56.1415198004600; Wed, 05 Nov 2014 06:33:24 -0800 (PST) Sender: are.you.winning@gmail.com Received: by 10.140.239.194 with HTTP; Wed, 5 Nov 2014 06:33:24 -0800 (PST) In-Reply-To: References: Date: Wed, 5 Nov 2014 14:33:24 +0000 X-Google-Sender-Auth: MBlca5KAYo-pS7P4s1UZ5cDZjT4 Message-ID: To: Jakub Zelenka Cc: Ferenc Kovacs , Juan Basso , PHP Internals Content-Type: multipart/alternative; boundary=001a11c230d05a92f505071d7644 Subject: Re: [PHP-DEV] Add a new flag for json_encode From: cw@daverandom.com (Chris Wright) --001a11c230d05a92f505071d7644 Content-Type: text/plain; charset=UTF-8 On 4 November 2014 17:07, Jakub Zelenka wrote: > On Tue, Nov 4, 2014 at 2:57 PM, Ferenc Kovacs wrote: > > > On Tue, Nov 4, 2014 at 4:13 AM, Juan Basso wrote: > > > > > Hi, > > > > > > I opened a pull request[1] in order to solve the bug 50224[2] and it > > ended > > > creating this pull request to add a new flag called > > > JSON_PRESERVE_FRACTIONAL_PART on json_encode function. This flag will > > make > > > the json encode to output float number always with decimal part, even > > when > > > it is 0. > > > > > > Currently if you try to convert 10.0 using json_encode it outputs 10. > It > > > means if you decode it it will give an integer instead a float. In PHP > > > words, json_decode(json_encode(10.0)) !== 10.0. > > > > > > After some researches and discussions it is not considered a bug > because > > > JSON specs treat integer and floats as number. Looking how other > > languages > > > treat this encoding I could find it: > > > - C (using lib jansson) and Ruby the output contains the decimal > portion; > > > - Python and Javascript outputs without the decimal portion. > > > > > > So it is kind of common to have different behaviors since JSON specs > > define > > > it as just number. The idea of the new flag is allow PHP to behave the > > both > > > ways. > > > > > > In the pull request Stanislav Malyshev suggested to merge it in the > 5.6, > > > but just want to see if someone else has any objection. Ferenc Kovacs > > > and Jakub Zelenka also are in favor of merging on 5.6. > > > Jakub completed suggesting to have this option enabled by default on > PHP > > 7. > > > > > > Anyone has any objection on merging it on 5.6? Some comments about > > enabling > > > it by default in 7? > > > > > > As a side note, with the pull request the encode of floats are about > 20% > > > faster, even after the flag check. This improvement just affect float > > > encoding and has no impact on the other types. > > > > > > [1] https://github.com/php/php-src/pull/642 > > > [2] https://bugs.php.net/bug.php?id=50224 > > > > > > > > > Thanks, > > > Juan Basso > > > > > > > Hi, > > > > just a slight correction: > > as far as I can tell, Jakus was ok with the general idea and even with > > turning this into the default, but he stated that it should only happen > in > > a major version (as it would cause a slight BC, maybe this could cause > > problems for some people who are expecting the zeroes to be truncated > from > > integer values) and he also stated that he thinks that there is no reason > > for a hurry to have this in a micro release, so this could wait until > 7.0. > > > > > Hey, > > Yeah exactly! > > I would really like to see it as default in the next major as it is a > useful feature. > > However I don't like adding new flag. The reason for that is that it > becomes useless after 7 is realesed. It would be useful only for >5.6.3 and > <7.0 though. The only use case in PHP 7 would be just to disable the flag > but that won't be very nice and completely different than other json > constant usage It means doing something like $options & > ~JSON_PRESERVE_FRACTIONAL_PART ... It's a bit messy IMHO. Think that it > would be much better to wait till 7 with this feature. > > I'm afraid I have to disagree here, I don't like the idea of changing this behaviour without making it controllable, and your logic is also slightly flawed - it's not that you'd have to do an & ~ to disable, it's that it wouldn't be enabled unless you added it to your flags argument. Passing a value of JSON_FORCE_OBJECT wouldn't be handled as JSON_FORCE_OBJECT | JSON_PRESERVE_FRACTIONAL_PART, you'd just need to explicitly pass 0 to the flags argument to disable it. The other advantage of making it into a flag is that any code that is already passing in flags (suggesting they need finer control over the output) will be unaffected by the change. > Cheers > > Jakub > --001a11c230d05a92f505071d7644--