File: person.cpp

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (295 lines) | stat: -rw-r--r-- 8,408 bytes parent folder | download | duplicates (2)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <benchmark/benchmark.h>
#include <rapidjson/document.h>
#include <simdjson.h>

#include <array>
#include <iostream>
#include <nlohmann/json.hpp>
#include <optional>
#include <rfl/json.hpp>
#include <type_traits>
#include <vector>

namespace person_read {

// ----------------------------------------------------------------------------

static const std::string json_string = R"(
    {
        "first_name": "Homer",
        "last_name": "Simpson",
        "children": [
            {
                "first_name": "Bart",
                "last_name": "Simpson",
                "children": []
            },
            {
                "first_name": "Lisa",
                "last_name": "Simpson",
                "children": []
            },
            {
                "first_name": "Maggie",
                "last_name": "Simpson",
                "children": []
            }
        ]
    }
)";

struct Person {
  std::string first_name;
  std::string last_name;
  std::vector<Person> children;
};

// ----------------------------------------------------------------------------
// nlohmann/json

std::vector<Person> nlohmann_to_children(const nlohmann::json &_val);

Person nlohmann_to_person(const nlohmann::json &_val);

std::vector<Person> nlohmann_to_children(const nlohmann::json &_arr) {
  std::vector<Person> children;
  for (const auto &val : _arr) {
    children.push_back(nlohmann_to_person(val));
  }
  return children;
}

Person nlohmann_to_person(const nlohmann::json &_val) {
  Person person;
  person.first_name = _val["first_name"].template get<std::string>();
  person.last_name = _val["last_name"].template get<std::string>();
  person.children = nlohmann_to_children(_val["children"]);
  return person;
}

static rfl::Result<Person> read_using_nlohmann() {
  try {
    auto val = nlohmann::json::parse(json_string);
    return nlohmann_to_person(val);
  } catch (std::exception &e) {
    return rfl::error(e.what());
  }
}

// ----------------------------------------------------------------------------
// rapidjson

std::vector<Person> rapidjson_to_children(const rapidjson::Value &_arr);

Person rapidjson_to_person(const rapidjson::Value &_val);

std::vector<Person> rapidjson_to_children(const rapidjson::Value &_arr) {
  std::vector<Person> children;
  for (auto it = _arr.Begin(); it != _arr.End(); ++it) {
    children.push_back(rapidjson_to_person(it->GetObject()));
  }
  return children;
}

Person rapidjson_to_person(const rapidjson::Value &_val) {
  Person person;
  person.first_name = _val["first_name"].GetString();
  person.last_name = _val["last_name"].GetString();
  person.children = rapidjson_to_children(_val["children"].GetObject());
  return person;
}

static rfl::Result<Person> read_using_rapidjson() {
  try {
    rapidjson::Document d;
    d.Parse(json_string.c_str());
    return rapidjson_to_person(d.GetObject());
  } catch (std::exception &e) {
    return rfl::error(e.what());
  }
}

// ----------------------------------------------------------------------------
// simdjson

std::vector<Person> simdjson_to_children(simdjson::ondemand::array _arr);

Person simdjson_to_person(simdjson::ondemand::object _val);

std::vector<Person> simdjson_to_children(simdjson::ondemand::array _arr) {
  std::vector<Person> children;
  for (auto val : _arr) {
    children.push_back(simdjson_to_person(val));
  }
  return children;
}

Person simdjson_to_person(simdjson::ondemand::object _val) {
  Person person;
  person.first_name = _val["first_name"].get_string().value();
  person.last_name = _val["last_name"].get_string().value();
  person.children = simdjson_to_children(_val["children"].get_array());
  return person;
}

static rfl::Result<Person> read_using_simdjson() {
  try {
    simdjson::ondemand::parser parser;
    auto padded_str = simdjson::padded_string(json_string);
    auto doc = parser.iterate(padded_str).value();
    return simdjson_to_person(doc.get_object());
  } catch (std::exception &e) {
    return rfl::error(e.what());
  }
}

// ----------------------------------------------------------------------------
// yyjson

rfl::Result<std::vector<Person>> yyjson_to_children(yyjson_val *_val);

rfl::Result<Person> yyjson_to_person(yyjson_val *_val);

rfl::Result<std::vector<Person>> yyjson_to_children(yyjson_val *_val) {
  std::vector<Person> children;
  yyjson_val *val;
  yyjson_arr_iter iter;
  yyjson_arr_iter_init(_val, &iter);
  while ((val = yyjson_arr_iter_next(&iter))) {
    auto r = yyjson_to_person(val);
    if (!r) {
      return error(r.error());
    }
    children.emplace_back(std::move(*r));
  }
  return children;
}

rfl::Result<Person> yyjson_to_person(yyjson_val *_val) {
  Person person;

  std::vector<rfl::Error> errors;
  std::array<bool, 3> found;
  std::fill(found.begin(), found.end(), false);

  yyjson_obj_iter iter;
  yyjson_obj_iter_init(_val, &iter);
  yyjson_val *key;
  while ((key = yyjson_obj_iter_next(&iter))) {
    const auto name = std::string_view(yyjson_get_str(key));
    const auto v = yyjson_obj_iter_get_val(key);
    if (!std::get<0>(found) && name == "first_name") {
      auto first_name = yyjson_get_str(v);
      if (first_name == NULL) {
        errors.push_back(rfl::Error(
            "Error reading 'first_name': Could not cast to string."));
        continue;
      }
      person.first_name = first_name;
      std::get<0>(found) = true;
    } else if (!std::get<1>(found) && name == "last_name") {
      auto last_name = yyjson_get_str(v);
      if (last_name == NULL) {
        errors.push_back(
            rfl::Error("Error reading 'last_name': Could not cast to string."));
        continue;
      }
      person.last_name = last_name;
      std::get<1>(found) = true;
    } else if (!std::get<2>(found) && name == "children") {
      auto children = yyjson_to_children(v);
      if (!children) {
        errors.push_back(
            rfl::Error("Error reading 'children': " + children.error().what()));
        continue;
      }
      person.children = std::move(*children);
      std::get<2>(found) = true;
    }
  }
  if (!std::get<0>(found)) {
    errors.push_back(rfl::Error("'first_name' not found"));
  }
  if (!std::get<1>(found)) {
    errors.push_back(rfl::Error("'last_name' not found"));
  }
  if (!std::get<2>(found)) {
    errors.push_back(rfl::Error("'children' not found"));
  }
  if (errors.size() != 0) {
    std::cout << "Some errors occurred:" << std::endl;
    for (const auto &err : errors) {
      std::cout << err.what() << std::endl;
    }
    return error(errors[0]);
  }
  return person;
}

static rfl::Result<Person> read_using_yyjson() {
  yyjson_doc *doc = yyjson_read(json_string.c_str(), json_string.size(), 0);
  if (!doc) {
    std::cout << "Could not parse document!" << std::endl;
    return rfl::error("Could not parse document");
  }
  yyjson_val *root = yyjson_doc_get_root(doc);
  auto person = yyjson_to_person(root);
  yyjson_doc_free(doc);
  return person;
}

// ----------------------------------------------------------------------------

static void BM_person_read_nlohmann(benchmark::State &state) {
  for (auto _ : state) {
    const auto res = read_using_nlohmann();
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_person_read_nlohmann);

static void BM_person_read_rapidjson(benchmark::State &state) {
  for (auto _ : state) {
    const auto res = read_using_rapidjson();
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_person_read_rapidjson);

static void BM_person_read_simdjson(benchmark::State &state) {
  for (auto _ : state) {
    const auto res = read_using_simdjson();
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_person_read_simdjson);

static void BM_person_read_yyjson(benchmark::State &state) {
  for (auto _ : state) {
    const auto res = read_using_yyjson();
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_person_read_yyjson);

static void BM_person_read_reflect_cpp(benchmark::State &state) {
  for (auto _ : state) {
    const auto res = rfl::json::read<Person>(json_string);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_person_read_reflect_cpp);

// ----------------------------------------------------------------------------

}  // namespace person_read