Skip to content

Commit

Permalink
pack: fix json floating point format regression (fluent#2592)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jpaskhay authored and xmcqueen committed Oct 6, 2020
1 parent d07e333 commit 4771776
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/flb_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,12 @@ static int msgpack2json(char *buf, int *off, size_t left,
case MSGPACK_OBJECT_FLOAT64:
{
char temp[512] = {0};
i = snprintf(temp, sizeof(temp)-1, "%.16g", o->via.f64);
if (o->via.f64 == (double)(long long int)o->via.f64) {
i = snprintf(temp, sizeof(temp)-1, "%.1f", o->via.f64);
}
else {
i = snprintf(temp, sizeof(temp)-1, "%.16g", o->via.f64);
}
ret = try_to_write(buf, off, left, temp, i);
}
break;
Expand Down

0 comments on commit 4771776

Please sign in to comment.