On 4 November 2014 17:07, Jakub Zelenka <bu...@php.net> wrote: > On Tue, Nov 4, 2014 at 2:57 PM, Ferenc Kovacs <tyr...@gmail.com> wrote: > > > On Tue, Nov 4, 2014 at 4:13 AM, Juan Basso <jrba...@gmail.com> 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 >