Skip to content

Commit 466ed04

Browse files
Merge pull request #17 from antithesishq/refactor-json-type
Refactor JSON type.
2 parents 891a02b + 2f5df4f commit 466ed04

File tree

1 file changed

+28
-38
lines changed

1 file changed

+28
-38
lines changed

antithesis_sdk.h

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,21 @@ namespace antithesis {
3333
}
3434
};
3535

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;
4038

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;
4341

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) {
4551
for (auto& pair : more_args) {
4652
(*this)[pair.first] = pair.second;
4753
}
@@ -264,7 +270,7 @@ namespace antithesis {
264270
template<class>
265271
inline constexpr bool always_false_v = false;
266272

267-
static std::ostream& operator<<(std::ostream& out, const BasicValueType& basic_value) {
273+
static std::ostream& operator<<(std::ostream& out, const JSONValue& json) {
268274
std::visit([&](auto&& arg)
269275
{
270276
using T = std::decay_t<decltype(arg)>;
@@ -285,58 +291,42 @@ namespace antithesis {
285291
out << arg;
286292
} else if constexpr (std::is_same_v<T, const char*>) {
287293
out << std::quoted(arg);
294+
} else if constexpr (std::is_same_v<T, std::nullptr_t>) {
295+
out << "null";
288296
} 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>) {
307297
out << arg;
308-
} else if constexpr (std::is_same_v<T, std::vector<BasicValueType>>) {
298+
} else if constexpr (std::is_same_v<T, JSONArray>) {
309299
out << '[';
310300
bool first = true;
311301
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;
317307
}
318308
out << ']';
319309
} else {
320-
static_assert(always_false_v<T>, "non-exhaustive ValueType visitor!");
310+
static_assert(always_false_v<T>, "non-exhaustive JSONValue visitor!");
321311
}
322-
}, value);
312+
}, json);
323313

324314
return out;
325315
}
326316

327317
static std::ostream& operator<<(std::ostream& out, const JSON& details) {
328-
out << "{ ";
318+
out << '{';
329319

330320
bool first = true;
331321
for (auto [key, value] : details) {
332322
if (!first) {
333-
out << ", ";
323+
out << ',';
334324
}
335-
out << std::quoted(key) << ": " << value;
325+
out << std::quoted(key) << ':' << value;
336326
first = false;
337327
}
338328

339-
out << " }";
329+
out << '}';
340330
return out;
341331
}
342332

0 commit comments

Comments
 (0)