-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsax_demo.cpp
More file actions
375 lines (290 loc) · 9.4 KB
/
Copy pathsax_demo.cpp
File metadata and controls
375 lines (290 loc) · 9.4 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <string_view>
#include <cassert>
#include <iostream>
#include <format>
#include <JsonFusion/serializer.hpp>
#include <JsonFusion/parser.hpp>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <map>
struct MyStruct {
};
template<> struct JsonFusion::Annotated<MyStruct> {
using Options = OptionsPack<JsonFusion::options::exclude>;
};
using BadAnnotated = JsonFusion::Annotated<int>;
BadAnnotated t_;
auto a = t_.value;
JsonFusion::Annotated<bool> bv;
auto b = bv.value;
struct T2 {
double f;
};
template<> struct JsonFusion::Annotated<T2> {
using Options = OptionsPack<
// JsonFusion::validators::range<0, M_PI>
>;
};
T2 tt;
auto tv = tt.f;
using MyAnnotatedStruct = JsonFusion::Annotated<MyStruct, JsonFusion::options::exclude>;
static_assert(JsonFusion::options::detail::annotation_meta_getter<MyAnnotatedStruct>::options
::template has_option<JsonFusion::options::detail::exclude_tag>);
static_assert(JsonFusion::options::detail::annotation_meta_getter<MyStruct>::options
::template has_option<JsonFusion::options::detail::exclude_tag>);
struct Vec_WithExternalMeta{
float x, y, z;
};
static_assert(JsonFusion::introspection::detail
::index_for_member_ptr<Vec_WithExternalMeta, &Vec_WithExternalMeta::y>() == 1);
template<> struct JsonFusion::Annotated<Vec_WithExternalMeta> {
using Options = OptionsPack<
JsonFusion::options::as_array
>;
};
template<> struct JsonFusion::AnnotatedField<Vec_WithExternalMeta, 1> {
using Options = OptionsPack<
JsonFusion::options::exclude
>;
};
static_assert(JsonFusion::options::detail::has_field_annotation_specialization_impl<Vec_WithExternalMeta, 1>::value);
using TstOpts = JsonFusion::options::detail::aggregate_field_opts_getter<Vec_WithExternalMeta, 1>;
static_assert(TstOpts::template has_option<JsonFusion::options::detail::exclude_tag>);
template <class ExternalVector = void>
struct Streamer {
struct Vector{
float x;
// JsonFusion::Annotated<float, JsonFusion::options::exclude> y;
float y;
float z;
};
// Each element will be serialized as a JSON array [x,y,z]
using VT = std::conditional_t<
std::is_same_v<ExternalVector, void>,
Vector,
Vec_WithExternalMeta
>;
using value_type = std::conditional_t<
std::is_same_v<ExternalVector, void>,
JsonFusion::Annotated<Vector, JsonFusion::options::as_array>,
Vec_WithExternalMeta
>;
mutable std::size_t * count;
mutable int counter = 0;
// Called by JsonFusion to pull the next element.
// Returns:
// value – v has been filled, keep going
// end – no more elements
// error – abort serialization
constexpr JsonFusion::stream_read_result read(VT & v) const {
if (counter >= *count) {
return JsonFusion::stream_read_result::end;
}
counter ++;
v.x = 42 + counter;
v.y = 43 + counter;
v.z = 44 + counter;
return JsonFusion::stream_read_result::value;
}
// Called at the start of the JSON array
void reset() const {
counter = 0;
}
void set_jsonfusion_context(std::size_t * c) const {
count = c;
}
};
static_assert(JsonFusion::ProducingStreamerLike<Streamer<>>, "Incompatible streamer");
void streaming_demo () {
{
struct TopLevel {
Streamer<> points_xyz;
};
TopLevel a;
std::size_t count =3;
std::string out;
JsonFusion::Serialize(a, out, &count);
std::cout << out << std::endl;
}
{
struct TopLevel {
Streamer<Vec_WithExternalMeta> points_xyz;
};
TopLevel a;
std::size_t count =3;
std::string out;
JsonFusion::Serialize(a, out, &count);
std::cout << out << std::endl;
}
/* Output:
{"points_xyz":[[43,44,45],[44,45,46],[45,46,47]]}
*/
}
void sax_demo() {
using JsonFusion::Annotated;
using namespace JsonFusion::options;
struct VectorWithTimestamp{
struct Vector{
float x, y, z;
};
Annotated<Vector, as_array> pos;
Annotated<std::uint64_t> timestamp;
};
struct PointsConsumer {
using value_type = Annotated<VectorWithTimestamp, as_array>;
void printPoint (const VectorWithTimestamp & point) {
std::cout << std::format("Point received: t={}, pos=({},{},{})",
point.timestamp.value,
point.pos->x,
point.pos->y,
point.pos->z
)
<< std::endl;
};
// Called at the start of the JSON array
void reset() {
std::cout << "Receiving points" << std::endl;
}
// Called for each element, with a fully-parsed value_type
bool consume(const VectorWithTimestamp & point) {
printPoint(point);
return true;
}
bool finalize(bool success) {
std::cout << "All points received" << std::endl;
return true;
}
};
static_assert(JsonFusion::ConsumingStreamerLike<PointsConsumer>, "Incompatible consumer");
struct TagsConsumer {
struct Tag {
std::string id;
std::string text;
};
using value_type = Tag;
// Called at the start of the JSON array
void reset() {
std::cout << "Receiving tags" << std::endl;
}
// Called for each element, with a fully-parsed value_type
bool consume(const Tag & tag) {
std::cout << tag.id << " " << tag.text << std::endl;
return true;
}
// called once at the end (with json success flag, if true, all data was consumed successfully)
bool finalize(bool success) {
if (!success) {
std::cout << "Tags stream aborted due to parse error\n";
return false;
}
std::cout << "Tags received\n";
return true;
}
};
static_assert(JsonFusion::ConsumingStreamerLike<TagsConsumer>, "Incompatible consumer");
struct TopLevel {
double f;
JsonFusion::Annotated<PointsConsumer, key<"timestamped_points">> positions;
TagsConsumer tags; // field name "tags" -> JSON key "tags"
};
TopLevel a;
std::string_view in(R"JSON(
{
"tags": [
{"id": "1", "text": "first tag"},
{"id": "2", "text": "second tag"}
],
"timestamped_points": [
[[1,2,3], 2],
[[4,5,6], 3],
[[7,8,9], 8]
],
"f": 3.18
})JSON");
JsonFusion::Parse(a, in);
/*
Receiving tags
1 first tag
2 second tag
Tags received
Receiving points
Point received: t=2, pos=(1,2,3)
Point received: t=3, pos=(4,5,6)
Point received: t=8, pos=(7,8,9)
All points received
*/
}
void nested_producers () {
struct StreamerOuter {
struct StreamerInner {
using value_type = double;
int count = 5;
mutable int counter = 0;
constexpr JsonFusion::stream_read_result read(double & v) const {
if (counter >= count) {
return JsonFusion::stream_read_result::end;
}
counter ++;
v = counter;
(*ctx_int) --;
return JsonFusion::stream_read_result::value;
}
void reset() const {
counter = 0;
}
mutable int * ctx_int;
void set_jsonfusion_context(int * ctx) const {
ctx_int = ctx;
}
};
using value_type = StreamerInner;
int count {8};
mutable int counter { 0};
constexpr JsonFusion::stream_read_result read(StreamerInner & v) const {
if (counter >= count) {
return JsonFusion::stream_read_result::end;
}
counter ++;
v.count = counter;
(*ctx_int) --;
return JsonFusion::stream_read_result::value;
}
void reset() const {
counter = 0;
}
mutable int * ctx_int;
void set_jsonfusion_context(int * ctx) const {
ctx_int = ctx;
}
};
std::string out;
int ctx = 100;
StreamerOuter s {};
s.ctx_int = &ctx;
JsonFusion::Serialize(s, out, &ctx);
std::cout << out << std::endl;
std::cout << ctx << std::endl;
/*
[[1],[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5],[1,2,3,4,5,6],[1,2,3,4,5,6,7],[1,2,3,4,5,6,7,8]]
*/
}
namespace fs = std::filesystem;
std::string read_file(const fs::path& filepath) {
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file) {
throw std::runtime_error(std::format("Failed to open file: {}", filepath.string()));
}
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::string buffer(size, '\0');
if (!file.read(buffer.data(), size)) {
throw std::runtime_error(std::format("Failed to read file: {}", filepath.string()));
}
return buffer;
}
int main(int argc, char ** argv) {
streaming_demo();
sax_demo();
nested_producers();
}