Re: JSON float number as string
| From: | Yasuo Ohgaki | Date: | Mon, 30 Mar 2015 01:25:29 +0000 |
| Subject: | Re: JSON float number as string | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-85536@lists.php.net to get a copy of this message | ||
Hi all,
On Mon, Mar 30, 2015 at 9:07 AM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote:
> Hi Jakub,
>
> On Mon, Mar 30, 2015 at 4:33 AM, Jakub Zelenka <bukka@php.net> wrote:
>
>> I would like to add a new option to JSON for dealing with large floats.
>> The
>> use case is mainly for decoder but can be used for encoder as well.
>>
>> JSON_FLOAT_AS_STRING
>> decode: all float values will be decoded as string
>> - It's often an issue for very large float values with many fractional
>> digits that are coming from platforms that support larger float
>> representation than double. In that case the conversion is lost and there
>> is no way how to get it back (see http://bugs.php.net/68456
>> [pls ignore
>> my
>> initial dump comments when I didn't get the issue :)] and an example of
>> the
>> lost precision here http://3v4l.org/80iCh ). Converting the
>> value to
>> string
>> keep the precision and resolves the problem.
>>
>> encode: all float values will be encoded as string
>> - re-using the constant for encoder makes sense if PHP creates JSON for
>> platform that support lower float type (e.g. C float) and the precision
>> loss is not acceptable
>>
>>
>> I think that this is more a bugfix as the precision is lost without any
>> way
>> how to get it back (except pre-processing json string with regular
>> expression). I would like to add it to 5.6.x if there are no objections?
>>
>
> Converting int/float to PHP native type is obvious bug.
> We must consider compatibility for released versions, so
> +1 for adding JSON_FLOAT_AS_STRING option.
>
> "int" should be fixed also.
> http://3v4l.org/95dHM
>
> So option may be JSON_SCALAR_AS_STRING or
> additional JSON_INT_AS_STRING.
>
> PHP7 should have string scalars by default. IMHO.
>
I didn't notice this bug before. This bug make me worry that new PHP7
type hint will create a lot of type casting bugs because even C programmer
makes this kind of design mistake...
If it's possible, coercive STH for weak type hint mode will help to reduce
type casting bugs. IMHO.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net