@@ -33,15 +33,21 @@ namespace antithesis {
33
33
}
34
34
};
35
35
36
- struct JSON ;
37
- typedef std::variant<std::string, bool , char , int , uint64_t , float , double , const char *, JSON> BasicValueType;
38
- typedef std::vector<BasicValueType> JSON_ARRAY;
39
- typedef std::variant<BasicValueType, JSON_ARRAY> ValueType;
36
+ struct JSON ; struct JSONArray ;
37
+ typedef std::variant<JSON, std::nullptr_t , std::string, bool , char , int , uint64_t , float , double , const char *, JSONArray> JSONValue;
40
38
41
- struct JSON : std::map<std::string, ValueType > {
42
- JSON ( std::initializer_list<std::pair< const std::string, ValueType>> args) : std::map<std::string, ValueType>(args) {}
39
+ struct JSONArray : std::vector<JSONValue > {
40
+ using std::vector<JSONValue>::vector;
43
41
44
- JSON ( std::initializer_list<std::pair<const std::string, ValueType>> args, std::vector<std::pair<const std::string, ValueType>> more_args ) : std::map<std::string, ValueType>(args) {
42
+ template <typename T, std::enable_if<std::is_convertible<T, JSONValue>::value, bool >::type = true >
43
+ JSONArray (std::vector<T> vals) : std::vector<JSONValue>(vals.begin(), vals.end()) {}
44
+ };
45
+
46
+ struct JSON : std::map<std::string, JSONValue> {
47
+ JSON () : std::map<std::string, JSONValue>() {}
48
+ JSON ( std::initializer_list<std::pair<const std::string, JSONValue>> args) : std::map<std::string, JSONValue>(args) {}
49
+
50
+ JSON ( std::initializer_list<std::pair<const std::string, JSONValue>> args, std::vector<std::pair<const std::string, JSONValue>> more_args ) : std::map<std::string, JSONValue>(args) {
45
51
for (auto & pair : more_args) {
46
52
(*this )[pair.first ] = pair.second ;
47
53
}
@@ -264,7 +270,7 @@ namespace antithesis {
264
270
template <class >
265
271
inline constexpr bool always_false_v = false ;
266
272
267
- static std::ostream& operator <<(std::ostream& out, const BasicValueType& basic_value ) {
273
+ static std::ostream& operator <<(std::ostream& out, const JSONValue& json ) {
268
274
std::visit ([&](auto && arg)
269
275
{
270
276
using T = std::decay_t <decltype (arg)>;
@@ -285,58 +291,42 @@ namespace antithesis {
285
291
out << arg;
286
292
} else if constexpr (std::is_same_v<T, const char *>) {
287
293
out << std::quoted (arg);
294
+ } else if constexpr (std::is_same_v<T, std::nullptr_t >) {
295
+ out << " null" ;
288
296
} else if constexpr (std::is_same_v<T, JSON>) {
289
- if (arg.empty ()) {
290
- out << " null" ;
291
- } else {
292
- out << arg;
293
- }
294
- } else {
295
- static_assert (always_false_v<T>, " non-exhaustive BasicValueType visitor!" );
296
- }
297
- }, basic_value);
298
-
299
- return out;
300
- }
301
-
302
- static std::ostream& operator <<(std::ostream& out, const ValueType& value) {
303
- std::visit ([&](auto && arg)
304
- {
305
- using T = std::decay_t <decltype (arg)>;
306
- if constexpr (std::is_same_v<T, BasicValueType>) {
307
297
out << arg;
308
- } else if constexpr (std::is_same_v<T, std::vector<BasicValueType> >) {
298
+ } else if constexpr (std::is_same_v<T, JSONArray >) {
309
299
out << ' [' ;
310
300
bool first = true ;
311
301
for (auto &item : arg) {
312
- if (!first) {
313
- out << ' ,' ;
314
- }
315
- first = false ;
316
- out << item;
302
+ if (!first) {
303
+ out << ' ,' ;
304
+ }
305
+ first = false ;
306
+ out << item;
317
307
}
318
308
out << ' ]' ;
319
309
} else {
320
- static_assert (always_false_v<T>, " non-exhaustive ValueType visitor!" );
310
+ static_assert (always_false_v<T>, " non-exhaustive JSONValue visitor!" );
321
311
}
322
- }, value );
312
+ }, json );
323
313
324
314
return out;
325
315
}
326
316
327
317
static std::ostream& operator <<(std::ostream& out, const JSON& details) {
328
- out << " { " ;
318
+ out << ' { ' ;
329
319
330
320
bool first = true ;
331
321
for (auto [key, value] : details) {
332
322
if (!first) {
333
- out << " , " ;
323
+ out << ' , ' ;
334
324
}
335
- out << std::quoted (key) << " : " << value;
325
+ out << std::quoted (key) << ' : ' << value;
336
326
first = false ;
337
327
}
338
328
339
- out << " } " ;
329
+ out << ' } ' ;
340
330
return out;
341
331
}
342
332
0 commit comments