From 9531de3835c6ddf4424f2b9251d080f8195c6000 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Tue, 23 Mar 2021 17:52:37 +0100 Subject: [PATCH] JsonEncode(): serialize integers w/o trailing .0 ... so Icinga DB can parse them as integers. --- lib/base/json.cpp | 20 +++++++++++++++++++- test/base-json.cpp | 8 ++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/base/json.cpp b/lib/base/json.cpp index d9e1718c2..41d1b6139 100644 --- a/lib/base/json.cpp +++ b/lib/base/json.cpp @@ -365,7 +365,25 @@ inline void JsonEncoder::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 diff --git a/test/base-json.cpp b/test/base-json.cpp index d97bd647c..811ac6183 100644 --- a/test/base-json.cpp +++ b/test/base-json.cpp @@ -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");