JsonEncode(): serialize integers w/o trailing .0

... so Icinga DB can parse them as integers.
This commit is contained in:
Alexander A. Klimov 2021-03-23 17:52:37 +01:00
parent 9830dc194b
commit 9531de3835
2 changed files with 23 additions and 5 deletions

View File

@ -365,7 +365,25 @@ inline
void JsonEncoder<prettyPrint>::NumberFloat(double value)
{
BeforeItem();
AppendJson(value);
// Make sure 0.0 is serialized as 0, so e.g. Icinga DB can parse it as int.
if (value < 0) {
long long i = value;
if (i == value) {
AppendJson(i);
} else {
AppendJson(value);
}
} else {
unsigned long long i = value;
if (i == value) {
AppendJson(i);
} else {
AppendJson(value);
}
}
}
template<bool prettyPrint>

View File

@ -31,11 +31,11 @@ BOOST_AUTO_TEST_CASE(encode)
],
"false": false,
"float": -1.25,
"int": -42.0,
"int": -42,
"null": null,
"string": "LF\nTAB\tAUml\u00e4Ill\ufffd",
"true": true,
"uint": 23.0
"uint": 23
}
)EOF");
@ -55,11 +55,11 @@ BOOST_AUTO_TEST_CASE(decode)
],
"false": false,
"float": -1.25,
"int": -42.0,
"int": -42,
"null": null,
"string": "LF\nTAB\tAUmlIll",
"true": true,
"uint": 23.0
"uint": 23
}
)EOF");