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
2ede8ae0
Commit
2ede8ae0
authored
Feb 22, 2021
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Omit @type fields for nested objects
parent
04a26387
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
10 deletions
+18
-10
libcaf_core/caf/json_writer.hpp
libcaf_core/caf/json_writer.hpp
+3
-0
libcaf_core/src/json_writer.cpp
libcaf_core/src/json_writer.cpp
+12
-4
libcaf_core/test/json_writer.cpp
libcaf_core/test/json_writer.cpp
+3
-6
No files found.
libcaf_core/caf/json_writer.hpp
View file @
2ede8ae0
...
...
@@ -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`.
...
...
libcaf_core/src/json_writer.cpp
View file @
2ede8ae0
...
...
@@ -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
()
{
...
...
libcaf_core/test/json_writer.cpp
View file @
2ede8ae0
...
...
@@ -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
}
...
...
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