Skip to content

Commit 8264895

Browse files
committed
fix #437: Set locale to C in jsonValue()
1 parent 6959372 commit 8264895

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/internal/reader_common.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,18 @@ Value jsonToValue(nlohmann::json value)
1313
{
1414
if (value.is_string())
1515
return value.get<std::string>();
16-
else if (value.is_number())
17-
return std::stod(value.dump());
18-
else if (value.is_boolean())
16+
else if (value.is_number()) {
17+
// Set locale to C to avoid conversion issues
18+
std::string oldLocale = std::setlocale(LC_NUMERIC, nullptr);
19+
std::setlocale(LC_NUMERIC, "C");
20+
21+
double converted = std::stod(value.dump());
22+
23+
// Restore old locale
24+
std::setlocale(LC_NUMERIC, oldLocale.c_str());
25+
26+
return converted;
27+
} else if (value.is_boolean())
1928
return value.get<bool>();
2029
else
2130
return value.dump();

test/load_project/load_project_test.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -709,3 +709,21 @@ TEST(LoadProjectTest, LoadNullDimensionMonitor)
709709
Project p(name);
710710
ASSERT_TRUE(p.load());
711711
}
712+
713+
TEST(LoadProjectTest, LoadDoubleValue)
714+
{
715+
// Regtest for #437
716+
std::string oldLocale = std::setlocale(LC_NUMERIC, nullptr);
717+
std::setlocale(LC_NUMERIC, "sk_SK.UTF-8");
718+
719+
std::string name = "regtest_projects/437_load_double_values.sb3";
720+
Project p(name);
721+
ASSERT_TRUE(p.load());
722+
723+
auto stage = p.engine()->stage();
724+
ASSERT_TRUE(stage);
725+
ASSERT_VAR(stage, "test");
726+
ASSERT_EQ(GET_VAR(stage, "test")->value().toDouble(), 5.6654);
727+
728+
std::setlocale(LC_NUMERIC, oldLocale.c_str());
729+
}
875 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)