diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index 1d84a2db..081ae0f4 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -272,8 +272,10 @@ struct Parser> { // ---------------------------------------------------------------------------- -template -struct Parser> { +/// Used for maps for which the key type is std::string. These are represented +/// as objects. +template +struct MapParser { public: using InputObjectType = typename ReaderType::InputObjectType; using InputVarType = typename ReaderType::InputVarType; @@ -281,12 +283,13 @@ struct Parser> { using OutputObjectType = typename WriterType::OutputObjectType; using OutputVarType = typename WriterType::OutputVarType; - /// Expresses the variables as a std::map. - static Result> read( - const ReaderType& _r, InputVarType* _var) noexcept { + using ValueType = std::decay_t; + + static Result read(const ReaderType& _r, + InputVarType* _var) noexcept { const auto get_pair = [&](auto _pair) { - const auto to_pair = [&](const ValueType& _val) { - return std::make_pair(_pair.first, _val); + const auto to_pair = [&](ValueType&& _val) { + return std::make_pair(_pair.first, std::move(_val)); }; return Parser>::read(_r, &_pair.second) @@ -294,25 +297,24 @@ struct Parser> { .value(); }; - const auto to_map = [&_r, get_pair](auto _obj) { - using namespace std::ranges::views; + const auto to_map = [&_r, get_pair](auto _obj) -> Result { auto m = _r.to_map(&_obj); - auto range = m | transform(get_pair); + MapType res; try { - return std::map(range.begin(), - range.end()); + for (const auto& p : m) { + res.insert(get_pair(p)); + } } catch (std::exception& e) { return Error(e.what()); } + return res; }; return _r.to_object(_var).and_then(to_map); } - /// Transform a std::map into an object - static OutputVarType write( - const WriterType& _w, - const std::map& _m) noexcept { + static OutputVarType write(const WriterType& _w, + const MapType& _m) noexcept { auto obj = _w.new_object(); for (const auto& [k, v] : _m) { _w.set_field( @@ -325,6 +327,15 @@ struct Parser> { } }; +template +struct Parser> + : public MapParser> {}; + +template +struct Parser> + : public MapParser> {}; + // ---------------------------------------------------------------------------- template diff --git a/tests/test_string_map.hpp b/tests/test_string_map.hpp new file mode 100644 index 00000000..b06cabf8 --- /dev/null +++ b/tests/test_string_map.hpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_string_map() { + std::cout << "test_string_map" << std::endl; + + std::map> homer; + homer.insert( + std::make_pair("firstName", std::make_unique("Homer"))); + homer.insert( + std::make_pair("lastName", std::make_unique("Simpson"))); + + write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})"); +} diff --git a/tests/test_string_unordered_map.hpp b/tests/test_string_unordered_map.hpp new file mode 100644 index 00000000..54238ec2 --- /dev/null +++ b/tests/test_string_unordered_map.hpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_string_unordered_map() { + std::cout << "test_string_unordered_map" << std::endl; + + using Map = std::unordered_map>; + Map homer; + homer.insert( + std::make_pair("firstName", std::make_unique("Homer"))); + homer.insert( + std::make_pair("lastName", std::make_unique("Simpson"))); + + // Unordered maps are unpredictable. We therefore only make sure that this + // compiles. + const auto json_string = rfl::json::write(homer); + const auto homer2 = rfl::json::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} diff --git a/tests/tests.cpp b/tests/tests.cpp index c196fea9..6f6afdd6 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -23,6 +23,8 @@ #include "test_replace_flatten.hpp" #include "test_replace_with_other_struct.hpp" #include "test_set.hpp" +#include "test_string_map.hpp" +#include "test_string_unordered_map.hpp" #include "test_tagged_union.hpp" #include "test_timestamp.hpp" #include "test_unique_ptr.hpp" @@ -59,6 +61,8 @@ int main() { test_unordered_multimap(); test_multiset(); test_unordered_multiset(); + test_string_map(); + test_string_unordered_map(); test_custom_class1(); test_custom_class2();