@@ -21,7 +21,7 @@ static_assert(glz::detail::reflectable<BasicStruct>);
21
21
// Demonstration of reading and writing a basic reflectable struct without `glz::meta`.
22
22
suite basic_reflection = [] {
23
23
" basic_struct_reflection" _test = [] {
24
- BasicStruct obj{42 , 3.14 , " Hello" , {1 ,2 , 3 }};
24
+ BasicStruct obj{42 , 3.14 , " Hello" , {1 , 2 , 3 }};
25
25
std::string json;
26
26
expect (not glz::write_json (obj, json));
27
27
expect (json == R"( {"i":42,"d":3.14,"str":"Hello","arr":[1,2,3]})" );
@@ -31,7 +31,7 @@ suite basic_reflection = [] {
31
31
expect (obj2.i == 42 );
32
32
expect (obj2.d == 3.14 );
33
33
expect (obj2.str == " Hello" );
34
- expect (obj2.arr == std::array<uint32_t ,3 >{1 ,2 , 3 });
34
+ expect (obj2.arr == std::array<uint32_t , 3 >{1 , 2 , 3 });
35
35
};
36
36
};
37
37
@@ -135,11 +135,11 @@ suite enum_test = [] {
135
135
// ------------------------------------
136
136
struct ContainerStruct
137
137
{
138
- std::vector<int > vec{1 ,2 , 3 };
139
- std::array<std::string, 2 > arr{" Hello" ," World" };
140
- std::tuple<int ,double ,std::string> tup{42 , 2.718 , " pi?" };
138
+ std::vector<int > vec{1 , 2 , 3 };
139
+ std::array<std::string, 2 > arr{" Hello" , " World" };
140
+ std::tuple<int , double , std::string> tup{42 , 2.718 , " pi?" };
141
141
std::deque<float > dq{3 .14f , 2 .71f };
142
- std::list<int > lis{10 ,11 ,12 };
142
+ std::list<int > lis{10 , 11 , 12 };
143
143
};
144
144
static_assert (glz::detail::reflectable<ContainerStruct>);
145
145
@@ -149,12 +149,13 @@ suite container_test = [] {
149
149
std::string json;
150
150
expect (not glz::write_json (c, json));
151
151
// Check output structure includes all
152
- expect (json == R"( {"vec":[1,2,3],"arr":["Hello","World"],"tup":[42,2.718,"pi?"],"dq":[3.14,2.71],"lis":[10,11,12]})" );
152
+ expect (json ==
153
+ R"( {"vec":[1,2,3],"arr":["Hello","World"],"tup":[42,2.718,"pi?"],"dq":[3.14,2.71],"lis":[10,11,12]})" );
153
154
154
155
ContainerStruct c2{};
155
156
expect (!glz::read_json (c2, json));
156
- expect (c2.vec == std::vector<int >{1 ,2 , 3 });
157
- expect (c2.lis == std::list<int >{10 ,11 ,12 });
157
+ expect (c2.vec == std::vector<int >{1 , 2 , 3 });
158
+ expect (c2.lis == std::list<int >{10 , 11 , 12 });
158
159
expect (std::get<2 >(c2.tup ) == " pi?" );
159
160
};
160
161
};
@@ -164,8 +165,8 @@ suite container_test = [] {
164
165
// ------------------------------------
165
166
struct MapStruct
166
167
{
167
- std::map<std::string,int > str_map{{" one" ,1 },{" two" ,2 }};
168
- std::unordered_map<int ,std::string> umap{{5 ," five" },{7 ," seven" }};
168
+ std::map<std::string, int > str_map{{" one" , 1 }, {" two" , 2 }};
169
+ std::unordered_map<int , std::string> umap{{5 , " five" }, {7 , " seven" }};
169
170
};
170
171
static_assert (glz::detail::reflectable<MapStruct>);
171
172
@@ -189,8 +190,14 @@ suite map_test = [] {
189
190
// ------------------------------------
190
191
// Variants
191
192
// ------------------------------------
192
- struct VarA { int x{}; };
193
- struct VarB { double y{}; };
193
+ struct VarA
194
+ {
195
+ int x{};
196
+ };
197
+ struct VarB
198
+ {
199
+ double y{};
200
+ };
194
201
195
202
template <>
196
203
struct glz ::meta<VarA>
@@ -248,7 +255,7 @@ struct PartialStruct
248
255
suite partial_test = [] {
249
256
" partial_write" _test = [] {
250
257
PartialStruct p{};
251
- static constexpr auto selected = glz::json_ptrs (" /a" ," /c" );
258
+ static constexpr auto selected = glz::json_ptrs (" /a" , " /c" );
252
259
std::string json;
253
260
expect (not glz::write_json<selected>(p, json));
254
261
expect (json == R"( {"a":1,"c":3})" );
@@ -296,7 +303,7 @@ struct RawExample
296
303
297
304
suite formatting_and_raw = [] {
298
305
" prettify_minify" _test = [] {
299
- PrettifyStruct pd{123 ," Hello" };
306
+ PrettifyStruct pd{123 , " Hello" };
300
307
std::string json;
301
308
expect (!glz::write_json (pd, json));
302
309
// Minify is default, let's prettify
@@ -321,25 +328,25 @@ suite formatting_and_raw = [] {
321
328
// ------------------------------------
322
329
struct PointerStruct
323
330
{
324
- std::array<int ,3 > arr{10 ,20 ,30 };
325
- std::map<std::string,std::string> m{{" key" ," value" }};
331
+ std::array<int , 3 > arr{10 , 20 , 30 };
332
+ std::map<std::string, std::string> m{{" key" , " value" }};
326
333
};
327
334
328
335
suite pointer_access = [] {
329
336
" json_pointer_get_set" _test = [] {
330
337
PointerStruct ps{};
331
- auto val = glz::get<int >(ps," /arr/1" );
338
+ auto val = glz::get<int >(ps, " /arr/1" );
332
339
expect (val.has_value ());
333
340
if (val) expect (val.value ().get () == 20 );
334
341
335
- glz::set (ps," /arr/1" ,42 );
342
+ glz::set (ps, " /arr/1" , 42 );
336
343
expect (ps.arr [1 ] == 42 );
337
344
338
- auto map_val = glz::get<std::string>(ps," /m/key" );
345
+ auto map_val = glz::get<std::string>(ps, " /m/key" );
339
346
expect (map_val.has_value ());
340
347
if (map_val) expect (map_val.value ().get () == " value" );
341
348
342
- glz::set (ps," /m/key" ," new_value" );
349
+ glz::set (ps, " /m/key" , " new_value" );
343
350
expect (ps.m [" key" ] == " new_value" );
344
351
};
345
352
};
@@ -357,17 +364,17 @@ struct NDJItem
357
364
358
365
suite ndjson_test = [] {
359
366
" ndjson_io" _test = [] {
360
- std::vector<NDJItem> items{{1 ," A" },{2 ," B" }};
367
+ std::vector<NDJItem> items{{1 , " A" }, {2 , " B" }};
361
368
362
369
std::string ndjson;
363
370
// NDJSON = each object on a newline
364
- expect (not glz::write <glz::opts{.format = glz::NDJSON}>(items, ndjson));
371
+ expect (not glz::write <glz::opts{.format = glz::NDJSON}>(items, ndjson));
365
372
// Should look like:
366
373
// {"x":1,"y":"A"}
367
374
// {"x":2,"y":"B"}
368
375
369
376
std::vector<NDJItem> read_back;
370
- expect (!glz::read <glz::opts{.format = glz::NDJSON}>(read_back, ndjson));
377
+ expect (!glz::read <glz::opts{.format = glz::NDJSON}>(read_back, ndjson));
371
378
expect (read_back.size () == 2 );
372
379
expect (read_back[0 ].x == 1 && read_back[0 ].y == " A" );
373
380
};
@@ -405,7 +412,7 @@ suite float_precision_test = [] {
405
412
FloatPrecision fp{};
406
413
std::string json;
407
414
// limit float precision to float32
408
- expect (not glz::write <glz::opts{.float_max_write_precision = glz::float_precision::float32}>(fp, json));
415
+ expect (not glz::write <glz::opts{.float_max_write_precision = glz::float_precision::float32}>(fp, json));
409
416
// This should produce fewer decimal places.
410
417
expect (json.find (" 3.1415927" ) != std::string::npos); // approximate float32 rounding
411
418
};
@@ -424,15 +431,18 @@ struct SchemaDemo
424
431
template <>
425
432
struct glz ::json_schema<SchemaDemo>
426
433
{
427
- schema x{.description = " An integer x" };
428
- schema name{.description = " A name for something" };
429
- schema flag{.description = " A boolean flag" };
434
+ schema x{.description = " An integer x" };
435
+ schema name{.description = " A name for something" };
436
+ schema flag{.description = " A boolean flag" };
430
437
};
431
438
432
439
suite schema_generation = [] {
433
440
" schema_demo" _test = [] {
434
441
auto schema = glz::write_json_schema<SchemaDemo>().value_or (" error" );
435
- expect (schema == R"( {"type":["object"],"properties":{"flag":{"$ref":"#/$defs/bool","description":"A boolean flag"},"name":{"$ref":"#/$defs/std::string","description":"A name for something"},"x":{"$ref":"#/$defs/int32_t","description":"An integer x"}},"additionalProperties":false,"$defs":{"bool":{"type":["boolean"]},"int32_t":{"type":["integer"],"minimum":-2147483648,"maximum":2147483647},"std::string":{"type":["string"]}}})" ) << schema;
442
+ expect (
443
+ schema ==
444
+ R"( {"type":["object"],"properties":{"flag":{"$ref":"#/$defs/bool","description":"A boolean flag"},"name":{"$ref":"#/$defs/std::string","description":"A name for something"},"x":{"$ref":"#/$defs/int32_t","description":"An integer x"}},"additionalProperties":false,"$defs":{"bool":{"type":["boolean"]},"int32_t":{"type":["integer"],"minimum":-2147483648,"maximum":2147483647},"std::string":{"type":["string"]}}})" )
445
+ << schema;
436
446
};
437
447
};
438
448
@@ -449,16 +459,19 @@ struct LocalSchema
449
459
450
460
struct glaze_json_schema
451
461
{
452
- glz::schema count{.description = " A count" };
453
- glz::schema file{.description = " A file path" };
454
- glz::schema valid{.description = " Validity flag" };
462
+ glz::schema count{.description = " A count" };
463
+ glz::schema file{.description = " A file path" };
464
+ glz::schema valid{.description = " Validity flag" };
455
465
};
456
466
};
457
467
458
468
suite local_schema_test = [] {
459
469
" local_schema" _test = [] {
460
470
auto schema = glz::write_json_schema<LocalSchema>().value_or (" error" );
461
- expect (schema == R"( {"type":["object"],"properties":{"count":{"$ref":"#/$defs/int32_t","description":"A count"},"file":{"$ref":"#/$defs/std::string","description":"A file path"},"valid":{"$ref":"#/$defs/bool","description":"Validity flag"}},"additionalProperties":false,"$defs":{"bool":{"type":["boolean"]},"int32_t":{"type":["integer"],"minimum":-2147483648,"maximum":2147483647},"std::string":{"type":["string"]}}})" ) << schema;
471
+ expect (
472
+ schema ==
473
+ R"( {"type":["object"],"properties":{"count":{"$ref":"#/$defs/int32_t","description":"A count"},"file":{"$ref":"#/$defs/std::string","description":"A file path"},"valid":{"$ref":"#/$defs/bool","description":"Validity flag"}},"additionalProperties":false,"$defs":{"bool":{"type":["boolean"]},"int32_t":{"type":["integer"],"minimum":-2147483648,"maximum":2147483647},"std::string":{"type":["string"]}}})" )
474
+ << schema;
462
475
};
463
476
};
464
477
0 commit comments