rfc:json_preserve_fractional_part
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:json_preserve_fractional_part [2014/12/25 17:45] – created jrbasso | rfc:json_preserve_fractional_part [2015/01/18 04:30] – jrbasso | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: JSON Preserve Fractional Part ====== | + | ====== PHP RFC: Preserve Fractional Part in JSON encode |
- | * Version: | + | * Version: |
* Date: 2014-12-25 | * Date: 2014-12-25 | ||
* Author: Juan Basso, jrbasso@gmail.com | * Author: Juan Basso, jrbasso@gmail.com | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 12: | Line 12: | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | Currently encoding float values like 10.0 to json encodes it as 10, it means that using json_decode | + | JSON specs define that any type of number is treated as a number, not differentiating integer, floats, etc. It means the number |
+ | |||
+ | In PHP, using json_encode | ||
+ | |||
+ | Testing the same behavior in other languages it shows that Go and JavaScript encode | ||
+ | |||
+ | The goal of this RFC is propose a new json_encode option called JSON_PRESERVE_ZERO_FRACTION to allow the same behavior of other languages and allowing the symmetry of the encode and decode. See below what changes: | ||
<PHP> | <PHP> | ||
// Currently | // Currently | ||
- | var_dump(json_decode(json_encode((float)10))); // Output int(10) | + | echo json_encode(10.0); |
+ | echo json_encode(10.1); | ||
+ | var_dump(json_decode(json_encode(10.0))); // Output int(10) | ||
+ | var_dump(10.0 === json_decode(json_encode(10.0))); | ||
// Proposed | // Proposed | ||
- | var_dump(json_decode(json_encode((float)10, JSON_PRESERVE_FRACTIONAL_PART))); // Output double(10) | + | echo json_encode(10.0); // Output 10 |
+ | echo json_encode(10.1); // Output 10.1 | ||
+ | echo json_encode(10.0, JSON_PRESERVE_ZERO_FRACTION); | ||
+ | echo json_encode(10.1, JSON_PRESERVE_ZERO_FRACTION); // Output 10.1 | ||
+ | var_dump(json_decode(json_encode(10.0, JSON_PRESERVE_ZERO_FRACTION))); // Output double(10) | ||
+ | var_dump(10.0 === json_decode(json_encode(10.0, | ||
</ | </ | ||
+ | |||
+ | A real case for this change is handling amounts, for example: | ||
+ | |||
+ | <PHP> | ||
+ | $order = [' | ||
+ | $encodeForSend = ' | ||
+ | // Send the JSON data | ||
+ | |||
+ | // After receive the JSON | ||
+ | $jsonReceived = ' | ||
+ | $order = json_decode($jsonReceived, | ||
+ | if ($order[' | ||
+ | // Do something | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | On the code above, the if statement will fail because the number is not float as set when it was sent. The solution would be change it to be '' | ||
+ | |||
+ | Having this option will give the option for developers to use it and increase the interoperability with other languages and also to increase the symmetry in PHP functions for cases like storing data in JSON. | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 39: | Line 72: | ||
==== New Constants ==== | ==== New Constants ==== | ||
- | - JSON_PRESERVE_FRACTIONAL_PART: Preserve the fraction part of float numbers with zero as decimal part. | + | - JSON_PRESERVE_ZERO_FRACTION: Preserve the fraction part of float numbers with zero as decimal part. |
+ | |||
+ | ==== Performance ==== | ||
+ | The implementation of this RFC changed how to encode float numbers and it affected the performance of the json_encode even when not using the new flag. | ||
+ | |||
+ | The table below shows the performance of the code below (PS: not using the new flag): | ||
+ | < | ||
+ | $number = 1.0; // Or 1.1 | ||
+ | $data = array_fill(0, | ||
+ | $iterations = 10000; | ||
+ | |||
+ | while ($iterations-- > 0) { | ||
+ | json_encode($data); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | +-------------------------------------------------------------------------------------+ | ||
+ | | Number | With changes | Elapsed time | Peak Memory (real = false) | | ||
+ | +-------------------------------------------------------------------------------------+ | ||
+ | | 1.0 | No | 2.6276361942291 | ||
+ | | 1.0 | Yes | 1.9471561908722 (25.9% faster) | 331 496 | | ||
+ | | 1.1 | No | 2.672217130661 | ||
+ | | 1.1 | Yes | 2.0137410163879 (24.6% faster) | 335 424 | | ||
+ | +-------------------------------------------------------------------------------------+ | ||
+ | </ | ||
+ | |||
+ | PS: Tests executed from Mac OS X 10.10.1 using CPU 2.3 GHz Intel Core i7 and 16GB of RAM. | ||
+ | |||
+ | PS 2: The changes made on this RFC doesn' | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
Line 46: | Line 108: | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
This RFC requires a 50%+1 majority. | This RFC requires a 50%+1 majority. | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | * Voting opened: 2015-01-11 02:55 UTC | ||
+ | * Voting closes: 2015-01-18 02:55 UTC | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== |
rfc/json_preserve_fractional_part.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1