Skip to content

Commit 348d183

Browse files
committed
Add initializer_list constructor to json_objectt
This new constructor facilitates the construction of instances of `json_objectt` which are const, because it gives a tidy way to construct an entire `json_objectt`.
1 parent c327572 commit 348d183

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/util/json.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ class json_objectt:public jsont
283283
{
284284
}
285285

286+
explicit json_objectt(
287+
std::initializer_list<typename objectt::value_type> initializer_list)
288+
: json_objectt{objectt{initializer_list}}
289+
{
290+
}
291+
286292
jsont &operator[](const std::string &key)
287293
{
288294
return object[key];

unit/util/json_object.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,30 @@ SCENARIO(
4646
}
4747
}
4848
}
49+
50+
SCENARIO(
51+
"Test that json_objectt can be constructed from an initializer list.",
52+
"[core][util][json]")
53+
{
54+
GIVEN("A json_objectt constructed from an initializer list.")
55+
{
56+
const json_objectt object{
57+
{"number", json_numbert{"6"}},
58+
{"string", json_stringt{"eggs"}},
59+
{"mice",
60+
json_objectt{{"number", json_numbert{"3"}},
61+
{"string", json_stringt{"blind"}}}}};
62+
THEN("The fields of the json_objectt match the initialiser list.")
63+
{
64+
REQUIRE(object["number"].kind == jsont::kindt::J_NUMBER);
65+
REQUIRE(object["number"].value == "6");
66+
REQUIRE(object["string"].kind == jsont::kindt::J_STRING);
67+
REQUIRE(object["string"].value == "eggs");
68+
const json_objectt mice = to_json_object(object["mice"]);
69+
REQUIRE(mice["number"].kind == jsont::kindt::J_NUMBER);
70+
REQUIRE(mice["number"].value == "3");
71+
REQUIRE(mice["string"].kind == jsont::kindt::J_STRING);
72+
REQUIRE(mice["string"].value == "blind");
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)