rfc:json_numeric_as_string
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:json_numeric_as_string [2015/05/04 10:50] – pasindu | rfc:json_numeric_as_string [2020/08/01 23:51] (current) – Status is "Declined" carusogabriel | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: JSON numeric as string ====== | ====== PHP RFC: JSON numeric as string ====== | ||
* Version: 0.1 | * Version: 0.1 | ||
- | * Date: 2015-04-09 | + | * Date: 2015-05-24 |
- | * Author: Jakub Zelenka, bukka@php.net , pasindu@php.net | + | * Author: Jakub Zelenka |
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | JSON is a data format that is often used for data exchange between different platforms. These platforms can have different number representation which might lead to problems like type inconsistency or worse in loosing information (e.g. issue with double precision) | + | JSON is a data format that is often used for data exchange between different platforms. These platforms can have different number representation which might lead to problems like type inconsistency or precision loss. |
===== Proposal ===== | ===== Proposal ===== | ||
- | The proposal | + | The proposal |
==== Float to string conversion ==== | ==== Float to string conversion ==== | ||
- | The first option called '' | + | The first option called '' |
=== Decoding === | === Decoding === | ||
- | The idea is to convert all float values to string | + | The idea is to convert all float values to string |
== Example == | == Example == | ||
Line 24: | Line 24: | ||
</ | </ | ||
- | will output: | + | outputs |
< | < | ||
array(2) { | array(2) { | ||
Line 33: | Line 34: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | == Use case == | ||
+ | |||
+ | The use case is preventing precision loss. Currently the float precision is always lost due to C double conversion from string. This is even bigger problem if the user operates with very large numbers. | ||
=== Encoding === | === Encoding === | ||
- | The idea is to convert all float values to string | + | The idea is to convert all float values to string |
== Example == | == Example == | ||
<code php> | <code php> | ||
- | ini_set(' | + | ini_set(' |
- | echo json_encode(array( "a"=>1.23343224234), JSON_FLOAT_AS_STRING); | + | echo json_encode(arrray(" |
</ | </ | ||
- | will output | + | outputs |
< | < | ||
Line 49: | Line 54: | ||
</ | </ | ||
+ | == Use case == | ||
+ | |||
+ | This might be useful when data are exported and then parsed by a parser that can't prevent of precision loss. That might be useful especially for platforms using C float type. | ||
==== Integer to string conversion ==== | ==== Integer to string conversion ==== | ||
- | The second option called '' | + | The second option called '' |
=== Decoding === | === Decoding === | ||
- | The idea is to convert all int values to string | + | The idea is to convert all int values to string |
== Example == | == Example == | ||
Line 71: | Line 79: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | == Use case == | ||
+ | |||
+ | This decoding option might be useful when type consistency is required. The problem with precision is already resolved by '' | ||
=== Encoding === | === Encoding === | ||
- | The idea is to convert all int values to string | + | The idea is to convert all int values to string |
== Example == | == Example == | ||
Line 86: | Line 98: | ||
</ | </ | ||
- | ===== Currently ===== | + | == Use case == |
- | <code php> | + | This encoding options might be useful when exporting integers greater than '' |
- | var_dump(json_decode('{" | + | |
- | </ | + | |
- | + | ||
- | Data has lost precision. One way to mitigate this is by doing some string manipulation, | + | |
- | + | ||
- | < | + | |
- | array(1) { | + | |
- | [" | + | |
- | float(1234567890.1235) | + | |
- | } | + | |
- | </ | + | |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 107: | Line 108: | ||
The proposed versions will be one of the voting options as some might consider it as not a self-contained feature. | The proposed versions will be one of the voting options as some might consider it as not a self-contained feature. | ||
- | There will be choice of 3 PHP version. The reason for that is that some might not consider the feature as self-contained addition. | + | There will be choice of 3 PHP version. |
- | - 5.6.next : this option | + | - 5.6.next : this voting |
- | - 7.0: this option | + | - 7.0: this voting |
- 7.1: this option will consider the feature/bug as a non self-contained addition | - 7.1: this option will consider the feature/bug as a non self-contained addition | ||
===== New Constants ===== | ===== New Constants ===== | ||
=== JSON_FLOAT_AS_STRING === | === JSON_FLOAT_AS_STRING === | ||
- | * json_decode - all float values | + | * json_decode - all float values |
- | * 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. Converting the value to string keep the precision and resolves the problem. | + | * json_encode - all float values |
- | * json_encode - all float values | + | |
- | * 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 | + | |
=== JSON_INT_AS_STRING === | === JSON_INT_AS_STRING === | ||
- | * json_decode - all int values | + | * json_decode - all int values |
- | * When decoding numbers that that are on the edge of max integer, | + | * json_encode - all int values |
- | * json_encode - all int values | + | |
- | * Re-using | + | |
+ | ===== Drawbacks ===== | ||
+ | |||
+ | It is important to note that the number | ||
+ | |||
+ | In addition, this proposal is not flexible as it has influence on all values | ||
+ | |||
+ | |||
+ | ===== Future replacement ===== | ||
+ | |||
+ | As noted in the Drawbacks section, this proposal is not flexible. There could be a better solution. That solution could be implementation of Json Schema that would address all drawbacks. However its introduction is only possible in the next minor version | ||
- | ===== Open Issues ===== | ||
- | Better names? | ||
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
This RFC is related only to JSON extension. | This RFC is related only to JSON extension. | ||
- | ===== Proposed | + | ===== Voting ===== |
- | There will be 5 voting: | + | |
- | - whether include '' | + | 50%+1 majority (see [[voting]]) for all votings. |
- | - whether include '' | + | |
- | - whether include '' | + | It started on 2015-06-09 at 20:00 UTC and ended on 2015-06-16 at 20:00 UTC. |
- | - whether include '' | + | |
- | - choice of the version | + | ==== JSON_FLOAT_TO_STRING for decoding |
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ==== JSON_FLOAT_TO_STRING for encoding | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ==== JSON_INT_TO_STRING for decoding | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ==== JSON_INT_TO_STRING for encoding | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ==== PHP version | ||
- | 50%+1 majority (see [[voting]]) for all votings | + | This option applies only if one of the above votes has 50%+1 votes |
+ | <doodle title=" | ||
+ | * PHP 5.6 | ||
+ | * PHP 7.0 | ||
+ | * PHP 7.1 | ||
+ | </ | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | The patch is really simple and will be implemented by the author of this RFC if it' | + | The patch is really simple and will be implemented by the author of this RFC if accepted. |
===== References ===== | ===== References ===== | ||
- | https:// | + | * Bug for float precision loss: https:// |
+ | * Initial discussion about introduction '' | ||
+ | * JSON Schema spec: http:// | ||
- | ===== Rejected Features ===== | ||
- | None |
rfc/json_numeric_as_string.1430736639.txt.gz · Last modified: 2017/09/22 13:28 (external edit)