Skip to content

Commit

Permalink
Made sure we can read larger integers, fixes #85
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Apr 18, 2024
1 parent bcf3483 commit 3f4def6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 4 additions & 2 deletions include/rfl/parsing/MapReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ class MapReader {
template <class T>
Result<T> key_to_numeric(auto& _pair) const noexcept {
try {
if constexpr (std::is_integral_v<T>) {
return static_cast<T>(std::stoi(_pair.first));
if constexpr (std::is_integral_v<T> && std::is_signed_v<T>) {
return static_cast<T>(std::stoll(_pair.first));
} else if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T>) {
return static_cast<T>(std::stoull(_pair.first));
} else if constexpr (std::is_floating_point_v<T>) {
return static_cast<T>(std::stod(_pair.first));
} else {
Expand Down
8 changes: 4 additions & 4 deletions tests/json/test_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ namespace test_map {
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::map<int, Person>> children;
std::unique_ptr<std::map<size_t, Person>> children;
};

void test() {
std::cout << std::source_location::current().function_name() << std::endl;

auto children = std::make_unique<std::map<int, Person>>();
auto children = std::make_unique<std::map<size_t, Person>>();
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
children->insert(std::make_pair(2660778562, Person{.first_name = "Maggie"}));

const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};

write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":{"1":{"firstName":"Bart","lastName":"Simpson"},"2":{"firstName":"Lisa","lastName":"Simpson"},"3":{"firstName":"Maggie","lastName":"Simpson"}}})");
R"({"firstName":"Homer","lastName":"Simpson","children":{"1":{"firstName":"Bart","lastName":"Simpson"},"2":{"firstName":"Lisa","lastName":"Simpson"},"2660778562":{"firstName":"Maggie","lastName":"Simpson"}}})");
}
} // namespace test_map

0 comments on commit 3f4def6

Please sign in to comment.