File: read-json.cpp

package info (click to toggle)
actor-framework 0.18.7-1~exp1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 8,740 kB
  • sloc: cpp: 85,162; sh: 491; python: 187; makefile: 11
file content (87 lines) | stat: -rw-r--r-- 2,388 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 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\nNote: 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)