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

Omit @type fields for nested objects

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