rfc:json_numeric_as_string

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:json_numeric_as_string [2015/05/08 17:48] bukkarfc: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 De Silva <pasindu@php.net>   * Author: Jakub Zelenka <bukka@php.net>, Pasindu De Silva <pasindu@php.net>
-  * Status: Draft+  * Status: Declined
   * First Published at: http://wiki.php.net/rfc/json_numeric_as_string   * First Published at: http://wiki.php.net/rfc/json_numeric_as_string
  
 ===== 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 is to introduce new options for dealing with this problem by converting the data to string.+The proposal introduces new options for dealing with the number conversion problem by converting data to string.
  
 ==== Float to string conversion ==== ==== Float to string conversion ====
-The first option called ''JSON_FLOAT_AS_STRING'' will convert all float values to string.+The first option called ''JSON_FLOAT_AS_STRING'' converts all float values to string.
  
 === Decoding === === Decoding ===
-The idea is to convert all float values to string during decoding. That will prevent loosing precision.+The idea is to convert all float values to string when decoding JSON string. That prevents of precision loss.
  
 == Example == == Example ==
Line 24: Line 24:
 </code> </code>
  
-will output:+outputs 
 <code> <code>
 array(2) { array(2) {
Line 34: Line 35:
 </code> </code>
  
-== Issue ==+== Use case ==
  
-Currently the float precision will be always lost due to float conversion. This is especially problem if user needs to save the whole precision.+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 during encoding.+The idea is to convert all float values to string when encoding PHP value.
  
 == Example == == Example ==
Line 47: Line 48:
 </code> </code>
  
-will output+outputs
  
 <code> <code>
Line 55: Line 56:
 == Use case == == Use case ==
  
-Such use case can be useful when data are exported and will be parsed by the parser that might loose precision. For example when exporting to platforms using C float type.+This might be useful when data are exported and then parsed by parser that can't prevent of precision lossThat might be useful especially for platforms using C float type.
  
 ==== Integer to string conversion ==== ==== Integer to string conversion ====
-The second option called ''JSON_INT_AS_STRING'' will convert all int values to string.+The second option called ''JSON_INT_AS_STRING'' converts all int values to string.
  
 === Decoding === === Decoding ===
-The idea is to convert all int values to string during decoding. That will prevent loosing precision.+The idea is to convert all int values to string when decoding JSON string.
  
 == Example == == Example ==
Line 81: Line 82:
 == Use case == == Use case ==
  
-This options might be useful when type consistency is required. The problem with precision is already resolved by ''JSON_BIGINT_AS_STRING''. However when the numbers are on the edge of ''INT_MAX'', then the result might be combination of string and integers.+This decoding option might be useful when type consistency is required. The problem with precision is already resolved by ''JSON_BIGINT_AS_STRING''. However when the numbers are on the edge of ''INT_MAX'', then the result is combination of strings and integers.
  
 === Encoding === === Encoding ===
-The idea is to convert all int values to string during encoding.+The idea is to convert all int values to string when encoding PHP value.
  
 == Example == == Example ==
Line 99: Line 100:
 == Use case == == Use case ==
  
-This options might be useful when exporting integers greater than 1 << 31 from 64bit platforms to 32bit platforms and the target JSON parser cannot handle such integers.+This encoding options might be useful when exporting integers greater than ''1 << 31'' from 64bit platforms to 32bit platforms and the target JSON parser cannot handle integer overflow.
  
 ===== 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 will consider the feature/bug as a self-contained small addition. The precedence for this is adding ''JSON_PRESERVE_ZERO_FRACTION''+  - 5.6.next : this voting option considers the feature/bug as a self-contained small addition. The precedence for this is an addition of ''JSON_PRESERVE_ZERO_FRACTION'' that was added in 5.6 cycle
-  - 7.0: this option will consider the feature/bug as a self-contained and but does not consider feature important for 5.6. It will be up to Release Manager to decide whether to merge it to 7.0.0 or 7.0.1.+  - 7.0: this voting option considers the feature/bug as a self-contained but it does not consider feature important for 5.6. It will be up to Release Manager to decide whether to merge it to 7.0.0 or 7.0.1.
   - 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 will be decoded as string +  * json_decode - all float values are 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. Converting the value to string keep the precision and resolves the problem. +  * json_encode - all float values are encoded as string
-  * json_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+
  
 === JSON_INT_AS_STRING === === JSON_INT_AS_STRING ===
-  * json_decode - all int values will be decoded as string +  * json_decode - all int values are decoded as string 
-    When decoding numbers that that are on the edge of max integer, the JSON_BIGINT_AS_STRING can produce int type for some number and string types for othersThis can be often inconvenient if the type consistency is required (e.g. further serialization/encoding for other platforms)  +  json_encode - all int values are encoded as string 
-  * json_encode - all int values will be encoded as string + 
-    * Re-using the constant for encoder makes sense if PHP creates JSON for platform that support lower int type (e.gfrom 64bit to 32bit platform) and decoder on remote platform can't handle bigger integers+ 
 +===== Drawbacks ===== 
 + 
 +It is important to note that the number of JSON options is limited to 31 values. There is still space for adding new ones as currently 11 options are used and the future ones can be duplicated for decoding and encodingHowever any new addition constant should be properly considered
 + 
 +In addition, this proposal is not flexible as it has influence on all values of the affected type. That might be unwanted behavior especially for ''JSON_INT_AS_STRING''. The only supported resulted type is a string which is another inflexibility.  
 + 
 + 
 +===== Future replacement ===== 
 + 
 +As noted in the Drawbacks section, this proposal is not flexibleThere could be a better solutionThat solution could be implementation of Json Schema that would address all drawbacks. However its introduction is only possible in the next minor version and the time for the implementation is considerably bigger
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
 This RFC is related only to JSON extension. This RFC is related only to JSON extension.
  
-===== Proposed Voting Choices ===== +===== Voting ===== 
-There will be 5 voting: + 
-  whether include ''JSON_FLOAT_TO_STRING'' for decoding +50%+1 majority (see [[voting]]) for all votings.  
-  - whether include ''JSON_FLOAT_TO_STRING'' for encoding + 
-  - whether include ''JSON_INT_TO_STRING'' for decoding +It started on 2015-06-09 at 20:00 UTC and ended on 2015-06-16 at 20:00 UTC.  
-  - whether include ''JSON_INT_TO_STRING'' for encoding + 
-  - choice of the version (see above)+==== JSON_FLOAT_TO_STRING for decoding ==== 
 + 
 +<doodle title="Include JSON_FLOAT_TO_STRING for decoding?" auth="bukka" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle> 
 + 
 +==== JSON_FLOAT_TO_STRING for encoding ==== 
 + 
 +<doodle title="Include JSON_FLOAT_TO_STRING for encoding?" auth="bukka" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle> 
 + 
 +==== JSON_INT_TO_STRING for decoding ==== 
 + 
 +<doodle title="Include JSON_INT_TO_STRING for decoding?" auth="bukka" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle> 
 + 
 +==== JSON_INT_TO_STRING for encoding ==== 
 + 
 +<doodle title="Include JSON_INT_TO_STRING for encoding?" auth="bukka" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle> 
 + 
 +==== 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 version used for the included constant" auth="bukka" voteType="single" closed="true">
 +   * PHP 5.6
 +   * PHP 7.0
 +   * PHP 7.1
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
-The patch is really simple and will be implemented by the author of this RFC if it'accepted.+The patch is really simple and will be implemented by the author of this RFC if accepted.
  
 ===== References ===== ===== References =====
-https://bugs.php.net/bug.php?id=68456+  * Bug for float precision loss: https://bugs.php.net/bug.php?id=68456 
 +  * Initial discussion about introduction ''JSON_FLOAT_TO_STRING'': http://bit.ly/1D2ZyAR 
 +  * JSON Schema spec: http://json-schema.org/
  
-===== Rejected Features ===== 
-None 
rfc/json_numeric_as_string.1431107289.txt.gz · Last modified: 2017/09/22 13:28 (external edit)