Commit 04a26387 authored by Dominik Charousset's avatar Dominik Charousset

Fix handling of member maps in the JSON reader

parent b6c394c5
......@@ -356,9 +356,10 @@ bool json_reader::begin_associative_array(size_t& size) {
FN_DECL;
return consume<false>(fn, [this, &size](const detail::json::value& val) {
if (val.data.index() == detail::json::value::object_index) {
auto& obj = get<detail::json::object>(val.data);
size = obj.size();
push(members{obj.begin(), obj.end()});
auto* obj = std::addressof(get<detail::json::object>(val.data));
pop();
size = obj->size();
push(members{obj->begin(), obj->end()});
return true;
} else {
emplace_error(sec::runtime_error, class_name, fn,
......
......@@ -347,6 +347,27 @@ bool inspect(Inspector& f, dummy_user& x) {
f.field("nickname", x.nickname));
}
struct phone_book {
std::string city;
std::map<std::string, int64_t> entries;
};
[[maybe_unused]] constexpr bool operator==(const phone_book& x,
const phone_book& y) noexcept {
return std::tie(x.city, x.entries) == std::tie(y.city, y.entries);
}
[[maybe_unused]] constexpr bool operator!=(const phone_book& x,
const phone_book& y) noexcept {
return !(x == y);
}
template <class Inspector>
bool inspect(Inspector& f, phone_book& x) {
return f.object(x).fields(f.field("city", x.city),
f.field("entries", x.entries));
}
// -- type IDs for for all unit test suites ------------------------------------
#define ADD_TYPE_ID(type) CAF_ADD_TYPE_ID(core_test, type)
......@@ -370,6 +391,7 @@ CAF_BEGIN_TYPE_ID_BLOCK(core_test, caf::first_custom_type_id)
ADD_TYPE_ID((int_actor))
ADD_TYPE_ID((level))
ADD_TYPE_ID((my_request))
ADD_TYPE_ID((phone_book))
ADD_TYPE_ID((point))
ADD_TYPE_ID((raw_struct))
ADD_TYPE_ID((rectangle))
......
......@@ -87,6 +87,12 @@ fixture::fixture() {
add_test_case(
R"_({"top-left":{"x":100,"y":200},"bottom-right":{"x":10,"y":20}})_",
rectangle{{100, 200}, {10, 20}});
add_test_case(R"({"@type": "phone_book",)"
R"( "city": "Model City",)"
R"( "entries": )"
R"({"Bob": 5556837,)"
R"( "Jon": 5559347}})",
phone_book{"Model City", {{"Bob", 5556837}, {"Jon", 5559347}}});
}
} // namespace
......
......@@ -175,6 +175,38 @@ SCENARIO("the JSON writer converts nested structs to strings") {
}
}
SCENARIO("the JSON writer converts structs with member dictionaries") {
GIVEN("a phone_book object") {
phone_book x;
x.city = "Model City";
x.entries["Bob"] = 555'6837;
x.entries["Jon"] = 555'9347;
WHEN("converting it to JSON with indentation factor 0") {
THEN("the JSON output is a single line") {
std::string out = R"({"@type": "phone_book",)"
R"( "city": "Model City",)"
R"( "entries": )"
R"({"Bob": 5556837,)"
R"( "Jon": 5559347}})";
CHECK_EQ(to_json_string(x, 0), out);
}
}
WHEN("converting it to JSON with indentation factor 2") {
THEN("the JSON output uses multiple lines") {
std::string out = R"({
"@type": "phone_book",
"city": "Model City",
"entries": {
"Bob": 5556837,
"Jon": 5559347
}
})";
CHECK_EQ(to_json_string(x, 2), out);
}
}
}
}
SCENARIO("the JSON writer omits or nulls missing values") {
GIVEN("a dummy_user object without nickname") {
dummy_user user;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment