@@ -33,15 +33,20 @@ 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<std::nullptr_t , std::string, bool , char , int , uint64_t , float , double , const char *, JSON, 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> requires std::convertible_to<T, JSONValue>
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::initializer_list<std::pair<const std::string, JSONValue>> args) : std::map<std::string, JSONValue>(args) {}
48
+
49
+ 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
50
for (auto & pair : more_args) {
46
51
(*this )[pair.first ] = pair.second ;
47
52
}
@@ -264,7 +269,7 @@ namespace antithesis {
264
269
template <class >
265
270
inline constexpr bool always_false_v = false ;
266
271
267
- static std::ostream& operator <<(std::ostream& out, const BasicValueType& basic_value ) {
272
+ static std::ostream& operator <<(std::ostream& out, const JSONValue& json ) {
268
273
std::visit ([&](auto && arg)
269
274
{
270
275
using T = std::decay_t <decltype (arg)>;
@@ -285,41 +290,25 @@ namespace antithesis {
285
290
out << arg;
286
291
} else if constexpr (std::is_same_v<T, const char *>) {
287
292
out << std::quoted (arg);
293
+ } else if constexpr (std::is_same_v<T, std::nullptr_t >) {
294
+ out << " null" ;
288
295
} 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
296
out << arg;
308
- } else if constexpr (std::is_same_v<T, std::vector<BasicValueType> >) {
297
+ } else if constexpr (std::is_same_v<T, JSONArray >) {
309
298
out << ' [' ;
310
299
bool first = true ;
311
300
for (auto &item : arg) {
312
- if (!first) {
313
- out << ' , ' ;
314
- }
315
- first = false ;
316
- out << item;
301
+ if (!first) {
302
+ out << " , " ;
303
+ }
304
+ first = false ;
305
+ out << item;
317
306
}
318
307
out << ' ]' ;
319
308
} else {
320
- static_assert (always_false_v<T>, " non-exhaustive ValueType visitor!" );
309
+ static_assert (always_false_v<T>, " non-exhaustive JSONValue visitor!" );
321
310
}
322
- }, value );
311
+ }, json );
323
312
324
313
return out;
325
314
}
0 commit comments