-
Notifications
You must be signed in to change notification settings - Fork 7.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed bug #50224 where float without decimals were converted to integer when encode to JSON #642
Closed
Closed
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4c263b6
Fixed bug #50224 where float without decimals were converted to integer
jrbasso 56b4ba5
Replacing use of strstr to strchr for performance reasons
jrbasso 9bb2248
Performance updates
jrbasso cbe1360
Performance optimizations
jrbasso 52a8fb0
Changed how float numbers are converted to JSON strings
jrbasso bb6697b
Fixing pointer address to avoid compiler warnings
jrbasso 408b7f7
Reducing the size of string for float to string conversion
jrbasso a64736e
Fixed typo
jrbasso 2468194
Using cast instead of getting the reference of first element
jrbasso dd52b7b
Using values from float.h and fallbacking to hardcoded value
jrbasso 8c41b03
Added JSON_PRESERVE_FRACTIONAL_PART option
jrbasso 4a53df1
Optimized the original code when the option JSON_PRESERVE_FRACTIONAL_…
jrbasso 8ff879d
Added a condition to avoid overflow
jrbasso 6c8d3dd
Renamed constant name after discussion on internals list
jrbasso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -422,6 +422,14 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR | |
if (!zend_isinf(d) && !zend_isnan(d)) { | ||
char *tmp; | ||
int l = spprintf(&tmp, 0, "%.*k", (int) EG(precision), d); | ||
if (strchr(tmp, '.') == NULL) { | ||
char *ntmp = (char *)emalloc(l + 3); | ||
strcpy(ntmp, tmp); | ||
strcat(ntmp, ".0"); | ||
len += 2; | ||
efree(tmp); | ||
tmp = ntmp; | ||
} | ||
smart_str_appendl(buf, tmp, l); | ||
efree(tmp); | ||
} else { | ||
|
@@ -630,6 +638,14 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_ | |
|
||
if (!zend_isinf(dbl) && !zend_isnan(dbl)) { | ||
len = spprintf(&d, 0, "%.*k", (int) EG(precision), dbl); | ||
if (strchr(d, '.') == NULL) { | ||
char *nd = (char *)emalloc(len + 3); | ||
strcpy(nd, d); | ||
strcat(nd, ".0"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. memcpy(nd + len, ".0", sizeof(".0")); |
||
len += 2; | ||
efree(d); | ||
d = nd; | ||
} | ||
smart_str_appendl(buf, d, len); | ||
efree(d); | ||
} else { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--TEST-- | ||
bug #50224 (json_encode() does not always encode a float as a float) | ||
--SKIPIF-- | ||
<?php if (!extension_loaded("json")) print "skip"; ?> | ||
--FILE-- | ||
<?php | ||
echo "* Testing JSON output\n\n"; | ||
var_dump(json_encode(12.3)); | ||
var_dump(json_encode(12)); | ||
var_dump(json_encode(12.0)); | ||
var_dump(json_encode(0.0)); | ||
var_dump(json_encode(array(12, 12.0, 12.3))); | ||
var_dump(json_encode((object)array('float' => 12.0, 'integer' => 12))); | ||
|
||
echo "\n* Testing encode/decode symmetry\n\n"; | ||
|
||
var_dump(json_decode(json_encode(12.3))); | ||
var_dump(json_decode(json_encode(12))); | ||
var_dump(json_decode(json_encode(12.0))); | ||
var_dump(json_decode(json_encode(0.0))); | ||
var_dump(json_decode(json_encode(array(12, 12.0, 12.3)))); | ||
var_dump(json_decode(json_encode((object)array('float' => 12.0, 'integer' => 12)))); | ||
var_dump(json_decode(json_encode((object)array('float' => 12.0, 'integer' => 12)), true)); | ||
?> | ||
--EXPECTF-- | ||
* Testing JSON output | ||
|
||
string(4) "12.3" | ||
string(2) "12" | ||
string(4) "12.0" | ||
string(3) "0.0" | ||
string(14) "[12,12.0,12.3]" | ||
string(27) "{"float":12.0,"integer":12}" | ||
|
||
* Testing encode/decode symmetry | ||
|
||
float(12.3) | ||
int(12) | ||
float(12) | ||
float(0) | ||
array(3) { | ||
[0]=> | ||
int(12) | ||
[1]=> | ||
float(12) | ||
[2]=> | ||
float(12.3) | ||
} | ||
object(stdClass)#%d (2) { | ||
["float"]=> | ||
float(12) | ||
["integer"]=> | ||
int(12) | ||
} | ||
array(2) { | ||
["float"]=> | ||
float(12) | ||
["integer"]=> | ||
int(12) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memcpy(nd, d, len);