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
5c9d462d
Unverified
Commit
5c9d462d
authored
Nov 10, 2021
by
Dominik Charousset
Committed by
GitHub
Nov 10, 2021
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1308
Add JSON reader example
parents
fe2f4d60
8980ba2d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
0 deletions
+90
-0
examples/CMakeLists.txt
examples/CMakeLists.txt
+3
-0
examples/config/read-json.cpp
examples/config/read-json.cpp
+87
-0
No files found.
examples/CMakeLists.txt
View file @
5c9d462d
...
@@ -18,6 +18,9 @@ endfunction()
...
@@ -18,6 +18,9 @@ endfunction()
add_core_example
(
. aout
)
add_core_example
(
. aout
)
add_core_example
(
. hello_world
)
add_core_example
(
. hello_world
)
# configuration API
add_core_example
(
config read-json
)
# basic message passing primitives
# basic message passing primitives
add_core_example
(
message_passing calculator
)
add_core_example
(
message_passing calculator
)
add_core_example
(
message_passing cell
)
add_core_example
(
message_passing cell
)
...
...
examples/config/read-json.cpp
0 → 100644
View file @
5c9d462d
// Illustrates how to read custom data types from JSON files.
#include "caf/actor_system.hpp"
#include "caf/actor_system_config.hpp"
#include "caf/caf_main.hpp"
#include "caf/json_reader.hpp"
#include "caf/type_id.hpp"
#include <fstream>
#include <iostream>
#include <string>
#include <string_view>
constexpr
std
::
string_view
example_input
=
R"([
{
"id": 1,
"name": "John Doe"
},
{
"id": 2,
"name": "Jane Doe",
"email": "jane@doe.com"
}
])"
;
struct
user
{
uint32_t
id
;
std
::
string
name
;
std
::
optional
<
std
::
string
>
email
;
};
template
<
class
Inspector
>
bool
inspect
(
Inspector
&
f
,
user
&
x
)
{
return
f
.
object
(
x
).
fields
(
f
.
field
(
"id"
,
x
.
id
),
f
.
field
(
"name"
,
x
.
name
),
f
.
field
(
"email"
,
x
.
email
));
}
using
user_list
=
std
::
vector
<
user
>
;
CAF_BEGIN_TYPE_ID_BLOCK
(
example_app
,
caf
::
first_custom_type_id
)
CAF_ADD_TYPE_ID
(
example_app
,
(
user
))
CAF_ADD_TYPE_ID
(
example_app
,
(
user_list
))
CAF_END_TYPE_ID_BLOCK
(
example_app
)
int
caf_main
(
caf
::
actor_system
&
sys
)
{
// Get file path from config (positional argument).
auto
&
cfg
=
sys
.
config
();
if
(
cfg
.
remainder
.
size
()
!=
1
)
{
std
::
cerr
<<
"*** expected one positional argument: path to a JSON file
\n
"
;
return
EXIT_FAILURE
;
}
auto
&
file_path
=
cfg
.
remainder
[
0
];
// Read file into a string.
std
::
ifstream
input
{
file_path
};
if
(
!
input
)
{
std
::
cerr
<<
"*** unable to open input file '"
<<
file_path
<<
"'
\n
"
;
return
EXIT_FAILURE
;
}
std
::
string
json
{
std
::
istreambuf_iterator
<
char
>
{
input
},
std
::
istreambuf_iterator
<
char
>
{}};
// Parse the JSON-formatted text.
caf
::
json_reader
reader
;
if
(
!
reader
.
load
(
json
))
{
std
::
cerr
<<
"*** failed to parse JSON input: "
<<
to_string
(
reader
.
get_error
())
<<
'\n'
;
return
EXIT_FAILURE
;
}
// Deserialize our user list from the parsed JSON.
user_list
users
;
if
(
!
reader
.
apply
(
users
))
{
std
::
cerr
<<
"*** failed to deserialize the user list: "
<<
to_string
(
reader
.
get_error
())
<<
"
\n\n
Note: expected a JSON list of user objects. For example:
\n
"
<<
example_input
<<
'\n'
;
return
EXIT_FAILURE
;
}
// Print the list in "CAF format".
std
::
cout
<<
"Entries loaded from file:
\n
"
;
for
(
auto
&
entry
:
users
)
std
::
cout
<<
"- "
<<
caf
::
deep_to_string
(
entry
)
<<
'\n'
;
return
EXIT_SUCCESS
;
}
CAF_MAIN
(
caf
::
id_block
::
example_app
)
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