Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
b6d1f482
Commit
b6d1f482
authored
Feb 18, 2022
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow JSON writers to skip @type annotations
parent
fc6ffc14
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
2 deletions
+36
-2
libcaf_core/caf/json_writer.hpp
libcaf_core/caf/json_writer.hpp
+16
-0
libcaf_core/src/json_writer.cpp
libcaf_core/src/json_writer.cpp
+1
-1
libcaf_core/test/json_writer.cpp
libcaf_core/test/json_writer.cpp
+19
-1
No files found.
libcaf_core/caf/json_writer.hpp
View file @
b6d1f482
...
...
@@ -38,6 +38,9 @@ public:
/// The default value for `skip_empty_fields()`.
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()`.
static
constexpr
string_view
field_type_suffix_default
=
"-type"
;
...
...
@@ -90,6 +93,16 @@ public:
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
/// fields. For example, CAF inserts field called "@foo${field_type_suffix}"
/// for a variant field called "foo".
...
...
@@ -264,6 +277,9 @@ private:
// fields as `$field: null` (false).
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
;
};
...
...
libcaf_core/src/json_writer.cpp
View file @
b6d1f482
...
...
@@ -97,7 +97,7 @@ bool json_writer::begin_object(type_id_t id, string_view name) {
pop
();
return
true
;
};
if
(
inside_object
())
if
(
inside_object
()
||
skip_object_type_annotation_
)
return
begin_associative_array
(
0
);
else
return
begin_associative_array
(
0
)
// Put opening paren, ...
...
...
libcaf_core/test/json_writer.cpp
View file @
b6d1f482
...
...
@@ -19,10 +19,13 @@ struct fixture {
expected
<
std
::
string
>
to_json_string
(
T
&&
x
,
size_t
indentation
,
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
;
writer
.
indentation
(
indentation
);
writer
.
skip_empty_fields
(
skip_empty_fields
);
writer
.
skip_object_type_annotation
(
skip_object_type_annotation
);
if
(
writer
.
apply
(
std
::
forward
<
T
>
(
x
)))
{
auto
buf
=
writer
.
str
();
return
{
std
::
string
{
buf
.
begin
(),
buf
.
end
()}};
...
...
@@ -129,6 +132,12 @@ SCENARIO("the JSON writer converts simple structs to strings") {
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"
)
{
THEN
(
"the JSON output uses multiple lines"
)
{
std
::
string
out
=
R"_({
...
...
@@ -139,6 +148,15 @@ SCENARIO("the JSON writer converts simple structs to strings") {
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
);
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment