-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathtest_dawjsonlink.cpp
135 lines (116 loc) · 3.94 KB
/
test_dawjsonlink.cpp
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
// The MIT License (MIT)
//
// Copyright (c) 2020 Darrell Wright
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files( the "Software" ), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and / or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <daw/json/daw_json_iterator.h>
#include <daw/json/daw_json_link.h>
#include <fstream>
#include <iostream>
#include <libnotify.h>
#include <sstream>
#include <string_view>
#ifdef __clang__
static constexpr auto COMPILER = "clang++";
#else
static constexpr auto COMPILER = "g++";
#endif
using namespace std;
struct coordinate_t {
double x;
double y;
double z;
auto operator<=>(const coordinate_t &) const = default;
friend ostream &operator<<(ostream &out, const coordinate_t &point) {
out << "coordinate_t {x: " << point.x << ", y: " << point.y
<< ", z: " << point.z << "}";
return out;
}
};
struct coordinates_t {
vector<coordinate_t> coordinates;
};
namespace daw::json {
template <> struct json_data_contract<coordinate_t> {
#ifdef __cpp_nontype_template_parameter_class
using type =
json_member_list<json_number<"x">, json_number<"y">, json_number<"z">>;
#else
constexpr inline static char const x[] = "x";
constexpr inline static char const y[] = "y";
constexpr inline static char const z[] = "z";
using type = json_member_list<json_number<x>, json_number<y>, json_number<z>>;
#endif
};
template <> struct json_data_contract<coordinates_t> {
#ifdef __cpp_nontype_template_parameter_class
using type = json_member_list<json_array<"coordinates", coordinate_t>>;
#else
constexpr inline static char const coordinates[] = "coordinates";
using type = json_member_list<json_array<coordinates, coordinate_t>>;
#endif
};
} // namespace daw::json
string read_file(const string &filename) {
ifstream file{filename};
if (!file.good()) {
return {};
}
return string{istreambuf_iterator<char>{file}, {}};
}
coordinate_t calc(const string &text) {
auto const json_sv = string_view(text.data(), text.size());
auto x = 0.0, y = 0.0, z = 0.0;
auto len = 0;
#ifdef UNCHECKED
using CheckedMode = daw::json::options::CheckedParseMode;
using range_t = daw::json::json_array_range<coordinate_t, CheckedMode::no>;
#else
using range_t = daw::json::json_array_range<coordinate_t>;
#endif
auto rng = range_t(json_sv, "coordinates");
for (auto c : rng) {
++len;
x += c.x;
y += c.y;
z += c.z;
}
return coordinate_t{x / len, y / len, z / len};
}
int main() {
auto right = coordinate_t{2.0, 0.5, 0.25};
for (auto v : {R"({"coordinates":[{"x":2.0,"y":0.5,"z":0.25}]})",
R"({"coordinates":[{"y":0.5,"x":2.0,"z":0.25}]})"}) {
auto left = calc(v);
if (left != right) {
cerr << left << " != " << right << endl;
exit(EXIT_FAILURE);
}
}
const auto &text = read_file("/tmp/1.json");
#ifdef UNCHECKED
const auto suffix = " NoCheck"s;
#else
const auto suffix = ""s;
#endif
const auto &results =
notifying_invoke([&]() { return calc(text); }, "C++/{} (DAW JSON Link{})",
COMPILER, suffix);
cout << results << endl;
}