diff --git a/docs/standard_containers.md b/docs/standard_containers.md index bd8bb819..8492ba22 100644 --- a/docs/standard_containers.md +++ b/docs/standard_containers.md @@ -2,8 +2,10 @@ The serialization and deserialization of containers is handled as follows: -`std::map` and `std::unordered_map` are treated -as objects. +`std::map`, `std::unordered_map`, +maps with any numeric types (integral and floating points) as keys, +and any objects where the `ReflectionType` is either a `std::string` or +a numeric type are treated as objects. Consider the following map: @@ -19,6 +21,35 @@ This will be represented as follows: {"firstName":"Homer","lastName":"Simpson"} ``` +The same is true for numeric types: + +```cpp +std::map homer; +homer[1] = "Homer"; +homer[2] = "Simpson"; +``` + +This will be represented as follows: + +```json +{"1":"Homer","2":"Simpson"} +``` + +If you wrap your keys into a `rfl::Validator`, this will be treated as an object as well: + +```cpp +using Between1and3 = rfl::Validator, rfl::Maximum<3>>; +std::map homer; +homer[1] = "Homer"; +homer[2] = "Simpson"; +``` + +This will also be represented as follows: + +```json +{"1":"Homer","2":"Simpson"} +``` + All other supported standard containers (other than `std::variant`, `std::optional`, `std::unique_ptr` and `std::shared_ptr`) will be represented as arrays. Containers for which the `value_type` diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index fc595ad3..4d99f7ce 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -282,28 +282,7 @@ struct MapParser { using ValueType = std::decay_t; static Result read(const R& _r, const InputVarType& _var) noexcept { - const auto get_pair = [&](auto& _pair) { - const auto to_pair = [&](ValueType&& _val) { - return std::make_pair(KeyType(std::move(_pair.first)), std::move(_val)); - }; - return Parser>::read(_r, _pair.second) - .transform(to_pair) - .value(); - }; - - const auto to_map = [&_r, get_pair](const auto& _obj) -> Result { - auto m = _r.to_map(_obj); - MapType res; - try { - for (auto& p : m) { - res.insert(get_pair(p)); - } - } catch (std::exception& e) { - return Error(e.what()); - } - return res; - }; - + const auto to_map = [&](const auto& _obj) { return make_map(_r, _obj); }; return _r.to_object(_var).and_then(to_map); } @@ -312,13 +291,88 @@ struct MapParser { for (const auto& [k, v] : _m) { auto parsed_val = Parser>::write(_w, v); if constexpr (internal::has_reflection_type_v) { - _w.set_field(k.reflection(), std::move(parsed_val), &obj); + using ReflT = typename KeyType::ReflectionType; + + if constexpr (std::is_integral_v || + std::is_floating_point_v) { + _w.set_field(std::to_string(k.reflection()), std::move(parsed_val), + &obj); + } else { + _w.set_field(k.reflection(), std::move(parsed_val), &obj); + } + + } else if constexpr (std::is_integral_v || + std::is_floating_point_v) { + _w.set_field(std::to_string(k), std::move(parsed_val), &obj); } else { _w.set_field(k, std::move(parsed_val), &obj); } } return obj; } + + private: + template + static Result key_to_numeric(auto& _pair) noexcept { + try { + if constexpr (std::is_integral_v) { + return static_cast(std::stoi(_pair.first)); + } else if constexpr (std::is_floating_point_v) { + return static_cast(std::stod(_pair.first)); + } else { + static_assert(always_false_v, "Unsupported type"); + } + } catch (std::exception& e) { + return Error(e.what()); + } + } + + static Result> make_key(auto& _pair) noexcept { + const auto to_pair = [&](auto _key) { + return std::make_pair(KeyType(std::move(_key)), std::move(_pair.second)); + }; + + if constexpr (std::is_integral_v || + std::is_floating_point_v) { + return key_to_numeric(_pair).transform(to_pair); + + } else if constexpr (internal::has_reflection_type_v) { + using ReflT = typename KeyType::ReflectionType; + + if constexpr (std::is_integral_v || + std::is_floating_point_v) { + return key_to_numeric(_pair).transform(to_pair); + } else { + return to_pair(_pair.first); + } + + } else { + return std::move(_pair); + } + } + + static std::pair get_pair(const R& _r, auto& _pair) { + const auto to_pair = [&](ValueType&& _val) { + auto pair = std::make_pair(std::move(_pair.first), std::move(_val)); + return make_key(pair); + }; + return Parser>::read(_r, _pair.second) + .and_then(to_pair) + .value(); + } + + static Result make_map(const R& _r, const auto& _obj) noexcept { + auto m = _r.to_map(_obj); + MapType res; + try { + for (auto& p : m) { + res.insert(get_pair(_r, p)); + } + } catch (std::exception& e) { + return Error(e.what()); + } + return res; + } }; template @@ -1179,9 +1233,15 @@ struct VectorParser { static constexpr bool treat_as_map() { if constexpr (is_map_like_not_multimap()) { if constexpr (internal::has_reflection_type_v) { - return std::is_same< - std::decay_t, - std::string>(); + using U = std::decay_t; + return std::is_same() || std::is_integral_v || + std::is_floating_point_v; + + // We do not need std::string here, it is already caught by the template + // specialization. + } else if constexpr (std::is_integral_v || + std::is_floating_point_v) { + return true; } else { return false; } diff --git a/tests/json/test_map.hpp b/tests/json/test_map.hpp index efb793bf..8f0df897 100644 --- a/tests/json/test_map.hpp +++ b/tests/json/test_map.hpp @@ -7,24 +7,24 @@ #include "write_and_read.hpp" void test_map() { - std::cout << "test_map" << std::endl; + std::cout << "test_map" << std::endl; - struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::unique_ptr>> - children = rfl::default_value; - }; + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::unique_ptr>> children = + rfl::default_value; + }; - auto children = std::make_unique>(); - 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"})); + auto children = std::make_unique>(); + 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"})); - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; + 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"}]]})"); + 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"}}})"); } diff --git a/tests/json/test_map_with_key_validation.hpp b/tests/json/test_map_with_key_validation.hpp new file mode 100644 index 00000000..192c2375 --- /dev/null +++ b/tests/json/test_map_with_key_validation.hpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_map_with_key_validation() { + std::cout << "test_map_with_key_validation" << std::endl; + + using Between1and3 = rfl::Validator, rfl::Maximum<3>>; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::unique_ptr>> + children = rfl::default_value; + }; + + auto children = std::make_unique>(); + + 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"})); + + 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"}}})"); +} diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index 2cbc0c13..081360dc 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -26,6 +26,7 @@ #include "test_literal_map.hpp" #include "test_literal_unordered_map.hpp" #include "test_map.hpp" +#include "test_map_with_key_validation.hpp" #include "test_monster_example.hpp" #include "test_multimap.hpp" #include "test_multiset.hpp" @@ -73,6 +74,7 @@ int main() { test_forward_list(); test_list(); test_map(); + test_map_with_key_validation(); test_unordered_map(); test_set(); test_unordered_set();