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/22 17:13] – bukka | 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 < | * 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) 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 when decoding JSON string. That will prevent loosing | + | The idea is to convert all float values to string when decoding JSON string. That prevents of precision |
== Example == | == Example == | ||
Line 24: | Line 24: | ||
</ | </ | ||
- | will output: | + | outputs |
< | < | ||
array(2) { | array(2) { | ||
Line 36: | Line 37: | ||
== Use case == | == Use case == | ||
- | The main use case is not loosing number | + | The use case is preventing |
=== Encoding === | === Encoding === | ||
- | The idea is to convert all float values to string | + | The idea is to convert all float values to string |
== Example == | == Example == | ||
Line 47: | Line 48: | ||
</ | </ | ||
- | will output | + | outputs |
< | < | ||
Line 55: | Line 56: | ||
== Use case == | == Use case == | ||
- | This might be useful when data are exported and will be parsed by the parser that might loose precision. | + | This might be useful when data are exported and then parsed by a parser that can't prevent of precision |
==== 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 81: | Line 82: | ||
== Use case == | == Use case == | ||
- | This decoding option might be useful when type consistency is required. The problem with precision is already resolved by '' | + | 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 99: | Line 100: | ||
== Use case == | == Use case == | ||
- | This encoding options might be useful when exporting integers greater than '' | + | This encoding options might be useful when exporting integers greater than '' |
===== 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 | ||
===== 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 to include '' | + | 50%+1 majority (see [[voting]]) for all votings. |
- | - whether to include '' | + | |
- | - whether to include '' | + | It started on 2015-06-09 at 20:00 UTC and ended on 2015-06-16 at 20:00 UTC. |
- | - whether to 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.txt · Last modified: 2020/08/01 23:51 by carusogabriel