Commit 2ede8ae0 authored by Dominik Charousset's avatar Dominik Charousset

Omit @type fields for nested objects

parent 04a26387
......@@ -201,6 +201,9 @@ private:
// Sets an error reason that the inspector failed to write a t.
void fail(type t);
// Checks whether any element in the stack has the type `object`.
bool inside_object() const noexcept;
// -- printing ---------------------------------------------------------------
// Adds a newline unless `compact() == true`.
......
......@@ -89,10 +89,13 @@ bool json_writer::begin_object(type_id_t, string_view name) {
pop();
return true;
};
return begin_associative_array(0) // Put opening paren, ...
&& begin_key_value_pair() // ... add implicit @type member, ..
&& add_type_annotation() // ... write content ...
&& end_key_value_pair(); // ... and wait for next field.
if (inside_object())
return begin_associative_array(0);
else
return begin_associative_array(0) // Put opening paren, ...
&& begin_key_value_pair() // ... add implicit @type member, ..
&& add_type_annotation() // ... write content ...
&& end_key_value_pair(); // ... and wait for next field.
}
bool json_writer::end_object() {
......@@ -500,6 +503,11 @@ void json_writer::fail(type t) {
emplace_error(sec::runtime_error, std::move(str));
}
bool json_writer::inside_object() const noexcept {
auto is_object = [](const entry& x) { return x.t == type::object; };
return std::any_of(stack_.begin(), stack_.end(), is_object);
}
// -- printing ---------------------------------------------------------------
void json_writer::nl() {
......
......@@ -147,10 +147,9 @@ SCENARIO("the JSON writer converts nested structs to strings") {
auto x = rectangle{{100, 200}, {10, 20}};
WHEN("converting it to JSON with indentation factor 0") {
THEN("the JSON output is a single line") {
std::string out
= R"({"@type": "rectangle", )"
R"("top-left": {"@type": "point", "x": 100, "y": 200}, )"
R"("bottom-right": {"@type": "point", "x": 10, "y": 20}})";
std::string out = R"({"@type": "rectangle", )"
R"("top-left": {"x": 100, "y": 200}, )"
R"("bottom-right": {"x": 10, "y": 20}})";
CHECK_EQ(to_json_string(x, 0), out);
}
}
......@@ -159,12 +158,10 @@ SCENARIO("the JSON writer converts nested structs to strings") {
std::string out = R"_({
"@type": "rectangle",
"top-left": {
"@type": "point",
"x": 100,
"y": 200
},
"bottom-right": {
"@type": "point",
"x": 10,
"y": 20
}
......
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