Commit b6d1f482 authored by Dominik Charousset's avatar Dominik Charousset

Allow JSON writers to skip @type annotations

parent fc6ffc14
...@@ -38,6 +38,9 @@ public: ...@@ -38,6 +38,9 @@ public:
/// The default value for `skip_empty_fields()`. /// The default value for `skip_empty_fields()`.
static constexpr bool skip_empty_fields_default = true; static constexpr bool skip_empty_fields_default = true;
/// The default value for `skip_object_type_annotation()`.
static constexpr bool skip_object_type_annotation_default = false;
/// The value value for `field_type_suffix()`. /// The value value for `field_type_suffix()`.
static constexpr string_view field_type_suffix_default = "-type"; static constexpr string_view field_type_suffix_default = "-type";
...@@ -90,6 +93,16 @@ public: ...@@ -90,6 +93,16 @@ public:
skip_empty_fields_ = value; skip_empty_fields_ = value;
} }
/// Returns whether the writer omits '@type' annotations for JSON objects.
[[nodiscard]] bool skip_object_type_annotation() const noexcept {
return skip_object_type_annotation_;
}
/// Configures whether the writer omits '@type' annotations for JSON objects.
void skip_object_type_annotation(bool value) noexcept {
skip_object_type_annotation_ = value;
}
/// Returns the suffix for generating type annotation fields for variant /// Returns the suffix for generating type annotation fields for variant
/// fields. For example, CAF inserts field called "@foo${field_type_suffix}" /// fields. For example, CAF inserts field called "@foo${field_type_suffix}"
/// for a variant field called "foo". /// for a variant field called "foo".
...@@ -264,6 +277,9 @@ private: ...@@ -264,6 +277,9 @@ private:
// fields as `$field: null` (false). // fields as `$field: null` (false).
bool skip_empty_fields_ = skip_empty_fields_default; bool skip_empty_fields_ = skip_empty_fields_default;
// Configures whether we omit the top-level '@type' annotation.
bool skip_object_type_annotation_ = false;
string_view field_type_suffix_ = field_type_suffix_default; string_view field_type_suffix_ = field_type_suffix_default;
}; };
......
...@@ -97,7 +97,7 @@ bool json_writer::begin_object(type_id_t id, string_view name) { ...@@ -97,7 +97,7 @@ bool json_writer::begin_object(type_id_t id, string_view name) {
pop(); pop();
return true; return true;
}; };
if (inside_object()) if (inside_object() || skip_object_type_annotation_)
return begin_associative_array(0); return begin_associative_array(0);
else else
return begin_associative_array(0) // Put opening paren, ... return begin_associative_array(0) // Put opening paren, ...
......
...@@ -19,10 +19,13 @@ struct fixture { ...@@ -19,10 +19,13 @@ struct fixture {
expected<std::string> expected<std::string>
to_json_string(T&& x, size_t indentation, to_json_string(T&& x, size_t indentation,
bool skip_empty_fields bool skip_empty_fields
= json_writer::skip_empty_fields_default) { = json_writer::skip_empty_fields_default,
bool skip_object_type_annotation
= json_writer::skip_object_type_annotation_default) {
json_writer writer; json_writer writer;
writer.indentation(indentation); writer.indentation(indentation);
writer.skip_empty_fields(skip_empty_fields); writer.skip_empty_fields(skip_empty_fields);
writer.skip_object_type_annotation(skip_object_type_annotation);
if (writer.apply(std::forward<T>(x))) { if (writer.apply(std::forward<T>(x))) {
auto buf = writer.str(); auto buf = writer.str();
return {std::string{buf.begin(), buf.end()}}; return {std::string{buf.begin(), buf.end()}};
...@@ -129,6 +132,12 @@ SCENARIO("the JSON writer converts simple structs to strings") { ...@@ -129,6 +132,12 @@ SCENARIO("the JSON writer converts simple structs to strings") {
CHECK_EQ(to_json_string(x, 0), out); CHECK_EQ(to_json_string(x, 0), out);
} }
} }
WHEN("converting it to JSON with indentation factor 0 and omitting @type") {
THEN("the JSON output is a single line") {
std::string out = R"_({"a": 10, "b": "foo"})_";
CHECK_EQ(to_json_string(x, 0, false, true), out);
}
}
WHEN("converting it to JSON with indentation factor 2") { WHEN("converting it to JSON with indentation factor 2") {
THEN("the JSON output uses multiple lines") { THEN("the JSON output uses multiple lines") {
std::string out = R"_({ std::string out = R"_({
...@@ -139,6 +148,15 @@ SCENARIO("the JSON writer converts simple structs to strings") { ...@@ -139,6 +148,15 @@ SCENARIO("the JSON writer converts simple structs to strings") {
CHECK_EQ(to_json_string(x, 2), out); CHECK_EQ(to_json_string(x, 2), out);
} }
} }
WHEN("converting it to JSON with indentation factor 2 and omitting @type") {
THEN("the JSON output uses multiple lines") {
std::string out = R"_({
"a": 10,
"b": "foo"
})_";
CHECK_EQ(to_json_string(x, 2, false, true), out);
}
}
} }
} }
......
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