Skip to content

Commit 631c6d3

Browse files
authored
pack: fix json floating point format regression (#2592)
The recent change to the JSON floating point formatting to use "%.16g" caused a regression where values that have no fractional part are formatted as integers. For example, "10.0" gets formatted as "10". This patch uses the same approach as https://github.com/ohler55/oj/blob/v3.10.13/ext/oj/dump_strict.c#L100-L101, which is used in Fluentd. It checks if the double value is equal to the integer part, and if so, will use "%.1f" as the format to ensure the decimal part is still rendered (with a single decimal place of ".0"). This prevents downstream datastores from having data type conflicts. This was tested by building locally and running through different value using the dummy input plugin and stdout output plugin with json_lines formatting. Will include example outputs of tests in Pull Request. Signed-off-by: Joey Paskhay <joey.paskhay@gmail.com>
1 parent 7dbfc0a commit 631c6d3

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/flb_pack.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,12 @@ static int msgpack2json(char *buf, int *off, size_t left,
478478
case MSGPACK_OBJECT_FLOAT64:
479479
{
480480
char temp[512] = {0};
481-
i = snprintf(temp, sizeof(temp)-1, "%.16g", o->via.f64);
481+
if (o->via.f64 == (double)(long long int)o->via.f64) {
482+
i = snprintf(temp, sizeof(temp)-1, "%.1f", o->via.f64);
483+
}
484+
else {
485+
i = snprintf(temp, sizeof(temp)-1, "%.16g", o->via.f64);
486+
}
482487
ret = try_to_write(buf, off, left, temp, i);
483488
}
484489
break;

0 commit comments

Comments
 (0)