This is an old revision of the document!
PHP RFC: JSON numeric as string
- Version: 0.1
- Date: 2015-04-09
- Author: Jakub Zelenka, bukka@php.net , pasindu@php.net
- Status: Draft
- First Published at: http://wiki.php.net/rfc/json_numeric_as_string
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)
Proposal
The proposal is to introduce new options for dealing with this problem by converting the data to string.
Float to string conversion
The first option called JSON_FLOAT_AS_STRING
will convert all float values to string.
Decoding
The idea is to convert all float values to string during decoding. That will prevent loosing precision.
Example
$data = json_decode('[1.23343224234, 1.34]', true, 512, JSON_FLOAT_AS_STRING); var_dump($data);
will output:
array(2) { [0] => string(13) "1.23343224234" [1] => string(4) "1.34" }
Encoding
The idea is to convert all float values to string during encoding.
Example
Integer to string conversion
The second option called JSON_INT_AS_STRING
will convert all int values to string.
Decoding
The idea is to convert all int values to string during decoding. That will prevent loosing precision.
Example
$data = json_decode('[23343224234, 34]', true, 512, JSON_INT_AS_STRING); var_dump($data);
will output:
array(2) { [0] => string(11) "23343224234" [1] => string(2) "34" }
Encoding
The idea is to convert all int values to string during encoding.
Example
Currently
var_dump(json_decode('{"test":1234567890.12345678}',true));
Data has lost precision. One way to mitigate this is by doing some string manipulation, else would be to make the json field test a string but would be sometimes out of the developers control.
array(1) { ["test"]=> float(1234567890.1235) }
Backward Incompatible Changes
The code that defines JSON_FLOAT_AS_STRING
and/or JSON_INT_AS_STRING
will be broken. However the constants have JSON_
prefix so their usage is not recommended and it should be relatively safe addition.
Proposed PHP Version(s)
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.
- 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
. - 7.0: this option will consider the feature/bug as a self-contained and but does not consider feature important for 5.6
- 7.1: this option will consider the feature/bug as a non self-contained addition
New Constants
JSON_FLOAT_AS_STRING
- json_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. Converting the value to string keep the precision and resolves the problem.
- 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_decode - all int values will be 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 others. This can be often inconvenient if the type consistency is required (e.g. further serialization/encoding for other platforms)
- 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.g. from 64bit to 32bit platform) and decoder on remote platform can't handle bigger integers
Open Issues
Better names?
Unaffected PHP Functionality
This RFC is related only to JSON extension.
Proposed Voting Choices
There will be 5 voting:
- whether include
JSON_FLOAT_TO_STRING
for decoding - whether include
JSON_FLOAT_TO_STRING
for encoding - whether include
JSON_INT_TO_STRING
for decoding - whether include
JSON_INT_TO_STRING
for encoding - choice of the version (see above)
50%+1 majority (see voting) for all votings
Patches and Tests
The patch is really simple and will be implemented by the author of this RFC if it's accepted.
References
Rejected Features
None