Re: Add a new flag for json_encode
| From: | Kris Craig | Date: | Sat, 29 Nov 2014 09:44:42 +0000 |
| Subject: | Re: Add a new flag for json_encode | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-79307@lists.php.net to get a copy of this message | ||
On Nov 3, 2014 7:13 PM, "Juan Basso" <jrbasso@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
Despite some tangential disagreements regarding the default behavior, there
does appear to be a general consensus with regard to the optional argument.
Could you please post an RFC for this if you haven't already? Thanks!
--Kris