Skip to content

Commit 95cef47

Browse files
committed
Porting implementation of RFC json_preserve_fractional_part
1 parent e6fb493 commit 95cef47

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

ext/json/json.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ static PHP_MINIT_FUNCTION(json)
102102
REGISTER_LONG_CONSTANT("JSON_PRETTY_PRINT", PHP_JSON_PRETTY_PRINT, CONST_CS | CONST_PERSISTENT);
103103
REGISTER_LONG_CONSTANT("JSON_UNESCAPED_UNICODE", PHP_JSON_UNESCAPED_UNICODE, CONST_CS | CONST_PERSISTENT);
104104
REGISTER_LONG_CONSTANT("JSON_PARTIAL_OUTPUT_ON_ERROR", PHP_JSON_PARTIAL_OUTPUT_ON_ERROR, CONST_CS | CONST_PERSISTENT);
105+
REGISTER_LONG_CONSTANT("JSON_PRESERVE_ZERO_FRACTION", PHP_JSON_PRESERVE_ZERO_FRACTION, CONST_CS | CONST_PERSISTENT);
105106

106107
REGISTER_LONG_CONSTANT("JSON_ERROR_NONE", PHP_JSON_ERROR_NONE, CONST_CS | CONST_PERSISTENT);
107108
REGISTER_LONG_CONSTANT("JSON_ERROR_DEPTH", PHP_JSON_ERROR_DEPTH, CONST_CS | CONST_PERSISTENT);

ext/json/json_encoder.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,18 @@ static inline void php_json_pretty_print_indent(smart_str *buf, int options) /*
9595

9696
/* }}} */
9797

98-
static inline void php_json_encode_double(smart_str *buf, double d) /* {{{ */
98+
static inline void php_json_encode_double(smart_str *buf, double d, int options) /* {{{ */
9999
{
100100
if (!zend_isinf(d) && !zend_isnan(d)) {
101101
size_t len;
102102
char num[PHP_JSON_DOUBLE_MAX_LENGTH];
103103
php_gcvt(d, EG(precision), '.', 'e', &num[0]);
104104
len = strlen(num);
105+
if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < PHP_JSON_DOUBLE_MAX_LENGTH - 2) {
106+
num[len++] = '.';
107+
num[len++] = '0';
108+
num[len] = '\0';
109+
}
105110
smart_str_appendl(buf, num, len);
106111
} else {
107112
JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN;
@@ -290,7 +295,7 @@ static void php_json_escape_string(smart_str *buf, char *s, size_t len, int opti
290295
if (type == IS_LONG) {
291296
smart_str_append_long(buf, p);
292297
} else if (type == IS_DOUBLE) {
293-
php_json_encode_double(buf, d);
298+
php_json_encode_double(buf, d, options);
294299
}
295300
return;
296301
}
@@ -502,7 +507,7 @@ void php_json_encode_zval(smart_str *buf, zval *val, int options) /* {{{ */
502507
break;
503508

504509
case IS_DOUBLE:
505-
php_json_encode_double(buf, Z_DVAL_P(val));
510+
php_json_encode_double(buf, Z_DVAL_P(val), options);
506511
break;
507512

508513
case IS_STRING:

ext/json/php_json.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ typedef enum {
6464
#define PHP_JSON_PRETTY_PRINT (1<<7)
6565
#define PHP_JSON_UNESCAPED_UNICODE (1<<8)
6666
#define PHP_JSON_PARTIAL_OUTPUT_ON_ERROR (1<<9)
67+
#define PHP_JSON_PRESERVE_ZERO_FRACTION (1<<10)
6768

6869
/* Internal flags */
6970
#define PHP_JSON_OUTPUT_ARRAY 0

ext/json/tests/bug50224.phpt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--TEST--
2+
bug #50224 (json_encode() does not always encode a float as a float)
3+
--SKIPIF--
4+
<?php if (!extension_loaded("json")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
echo "* Testing JSON output\n\n";
8+
var_dump(json_encode(12.3, JSON_PRESERVE_ZERO_FRACTION));
9+
var_dump(json_encode(12, JSON_PRESERVE_ZERO_FRACTION));
10+
var_dump(json_encode(12.0, JSON_PRESERVE_ZERO_FRACTION));
11+
var_dump(json_encode(0.0, JSON_PRESERVE_ZERO_FRACTION));
12+
var_dump(json_encode(array(12, 12.0, 12.3), JSON_PRESERVE_ZERO_FRACTION));
13+
var_dump(json_encode((object)array('float' => 12.0, 'integer' => 12), JSON_PRESERVE_ZERO_FRACTION));
14+
15+
echo "\n* Testing encode/decode symmetry\n\n";
16+
17+
var_dump(json_decode(json_encode(12.3, JSON_PRESERVE_ZERO_FRACTION)));
18+
var_dump(json_decode(json_encode(12, JSON_PRESERVE_ZERO_FRACTION)));
19+
var_dump(json_decode(json_encode(12.0, JSON_PRESERVE_ZERO_FRACTION)));
20+
var_dump(json_decode(json_encode(0.0, JSON_PRESERVE_ZERO_FRACTION)));
21+
var_dump(json_decode(json_encode(array(12, 12.0, 12.3), JSON_PRESERVE_ZERO_FRACTION)));
22+
var_dump(json_decode(json_encode((object)array('float' => 12.0, 'integer' => 12), JSON_PRESERVE_ZERO_FRACTION)));
23+
var_dump(json_decode(json_encode((object)array('float' => 12.0, 'integer' => 12), JSON_PRESERVE_ZERO_FRACTION), true));
24+
?>
25+
--EXPECTF--
26+
* Testing JSON output
27+
28+
string(4) "12.3"
29+
string(2) "12"
30+
string(4) "12.0"
31+
string(3) "0.0"
32+
string(14) "[12,12.0,12.3]"
33+
string(27) "{"float":12.0,"integer":12}"
34+
35+
* Testing encode/decode symmetry
36+
37+
float(12.3)
38+
int(12)
39+
float(12)
40+
float(0)
41+
array(3) {
42+
[0]=>
43+
int(12)
44+
[1]=>
45+
float(12)
46+
[2]=>
47+
float(12.3)
48+
}
49+
object(stdClass)#%d (2) {
50+
["float"]=>
51+
float(12)
52+
["integer"]=>
53+
int(12)
54+
}
55+
array(2) {
56+
["float"]=>
57+
float(12)
58+
["integer"]=>
59+
int(12)
60+
}

0 commit comments

Comments
 (0)