Skip to content

Commit 16ab889

Browse files
committed
fix #488: Use Value constructor for numbers in jsonToValue()
1 parent 4f51860 commit 16ab889

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

src/internal/reader_common.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,7 @@ Value jsonToValue(nlohmann::json value)
1313
{
1414
if (value.is_string())
1515
return value.get<std::string>();
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())
16+
else if (value.is_boolean())
2817
return value.get<bool>();
2918
else
3019
return value.dump();

test/load_project/load_project_test.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ TEST(LoadProjectTest, LoadTestProject)
231231

232232
// Stage: lists
233233
ASSERT_LIST(stage, "list1");
234-
ASSERT_EQ(GET_LIST(stage, "list1")->toString(), "3 1 4 8 7 6 7 7 2 4");
234+
ASSERT_EQ(GET_LIST(stage, "list1")->size(), 10);
235+
ASSERT_EQ(GET_LIST(stage, "list1")->toString(), "3148767724");
235236

236237
// Sprite1
237238
ASSERT_NE(engine->findTarget("Sprite1"), -1);
@@ -282,7 +283,8 @@ TEST(LoadProjectTest, LoadTestProject)
282283

283284
// Sprite1: lists
284285
ASSERT_LIST(sprite1, "list2");
285-
ASSERT_EQ(GET_LIST(sprite1, "list2")->toString(), "9 6 3 8 0 4 4 2 9 7");
286+
ASSERT_EQ(GET_LIST(sprite1, "list2")->size(), 10);
287+
ASSERT_EQ(GET_LIST(sprite1, "list2")->toString(), "9638044297");
286288

287289
// Sprite1: blocks
288290
ASSERT_EQ(sprite1->greenFlagBlocks().size(), 1);
@@ -445,7 +447,8 @@ TEST(LoadProjectTest, LoadTestProject)
445447

446448
// Balloon1: lists
447449
ASSERT_LIST(sprite2, "list2");
448-
ASSERT_EQ(GET_LIST(sprite2, "list2")->toString(), "0 4 3 4 1 5 6 9 4 8");
450+
ASSERT_EQ(GET_LIST(sprite2, "list2")->size(), 10);
451+
ASSERT_EQ(GET_LIST(sprite2, "list2")->toString(), "0434156948");
449452

450453
// Monitors
451454
const auto &monitors = engine->monitors();
@@ -740,3 +743,19 @@ TEST(LoadProjectTest, LoadDoubleValue)
740743

741744
std::setlocale(LC_NUMERIC, oldLocale.c_str());
742745
}
746+
747+
TEST(LoadProjectTest, LoadScientificNotationNumber)
748+
{
749+
// Regtest for #488
750+
std::string name = "regtest_projects/488_scientific_notation_conversion.sb3";
751+
Project p(name);
752+
ASSERT_TRUE(p.load());
753+
754+
auto stage = p.engine()->stage();
755+
ASSERT_TRUE(stage);
756+
ASSERT_VAR(stage, "test1");
757+
ASSERT_EQ(GET_VAR(stage, "test1")->value().toDouble(), 5000);
758+
759+
ASSERT_VAR(stage, "test2");
760+
ASSERT_EQ(GET_VAR(stage, "test2")->value().toDouble(), 0);
761+
}
Binary file not shown.

0 commit comments

Comments
 (0)