Fix conversion of real numbers from json

This commit is contained in:
Emmanuel Durand
2026-02-20 19:47:13 -05:00
parent f72a3b20ae
commit 6cbd5a04f8
+14 -8
View File
@@ -346,6 +346,12 @@ bool loadJsonFile(const std::string& filename, Json::Value& configuration)
/*************/
Values jsonToValues(const Json::Value& values)
{
// When converting from json to values, we start
// by trying to convert to the more complex type
// then we go down. So we start with double, then
// integer and then boolean. This prevents converting
// double to integer by error, for instance when
// handling the value `1.0`.
Values outValues;
if (values.isBool())
@@ -358,12 +364,12 @@ Values jsonToValues(const Json::Value& values)
{
for (const auto& v : values)
{
if (v.isBool())
outValues.emplace_back(v.asBool());
if (v.isDouble())
outValues.emplace_back(v.asFloat());
else if (v.isInt())
outValues.emplace_back(v.asInt());
else if (v.isDouble())
outValues.emplace_back(v.asFloat());
else if (v.isBool())
outValues.emplace_back(v.asBool());
else if (v.isArray() || v.isObject())
outValues.emplace_back(jsonToValues(v));
else
@@ -376,12 +382,12 @@ Values jsonToValues(const Json::Value& values)
int index = 0;
for (const auto& v : values)
{
if (v.isBool())
outValues.emplace_back(v.asBool(), names[index]);
if (v.isDouble())
outValues.emplace_back(v.asFloat(), names[index]);
else if (v.isInt())
outValues.emplace_back(v.asInt(), names[index]);
else if (v.isDouble())
outValues.emplace_back(v.asFloat(), names[index]);
else if (v.isBool())
outValues.emplace_back(v.asBool(), names[index]);
else if (v.isArray() || v.isObject())
outValues.emplace_back(jsonToValues(v), names[index]);
else