mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-02 16:16:19 +08:00
Made sure we can read larger integers, fixes #85
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user