108 lines
5.4 KiB
C++
108 lines
5.4 KiB
C++
// #pragma once
|
|
|
|
// #define _TO_JSON_FIELD(field) j[#field] = t.field;
|
|
// #define _FROM_JSON_FIELD(field) bts::io::JsonReader{&j, #field}.read(t.field);
|
|
|
|
// #define SPARK_TO_JSON_IMPL(type, for_each) \
|
|
// inline void to_json(nlohmann::json &j, const type &t) { \
|
|
// FOR_EACH(_TO_JSON_FIELD) \
|
|
// }
|
|
|
|
// #define SPARK_FROM_JSON_IMPL(type, for_each) \
|
|
// inline void from_json(const nlohmann::json &j, type &t) { \
|
|
// FOR_EACH(_FROM_JSON_FIELD) \
|
|
// }
|
|
|
|
// #define OSTREAM_JSON_IMPL(type) \
|
|
// inline std::ostream &operator<<(std::ostream &out, const type &t) { \
|
|
// nlohmann::json j; \
|
|
// to_json(j, t); \
|
|
// out << j.dump(2); \
|
|
// return out; \
|
|
// }
|
|
|
|
#pragma once
|
|
|
|
#include "system/json/json_reader.h"
|
|
|
|
#define _JSON_IMPL_WRITE_JSON(name) j[#name] = src.name;
|
|
#define TO_JSON_IMPL(type, for_all) \
|
|
inline void to_json(nlohmann::json &j, const type &src) { \
|
|
for_all(_JSON_IMPL_WRITE_JSON) \
|
|
}
|
|
#define _JSON_OPTIONAL_IMPL_WRITE_JSON(field) \
|
|
if (src.field) { \
|
|
j[#field] = *src.field; \
|
|
}
|
|
#define TO_JSON_OPTIONAL_IMPL(type, for_all) \
|
|
inline void to_json(nlohmann::json &j, const type &src) { \
|
|
for_all(_JSON_OPTIONAL_IMPL_WRITE_JSON) \
|
|
}
|
|
|
|
#define _JSON_IMPL_READ_JSON(name) jr.read(#name, dest.name);
|
|
#define FROM_JSON_IMPL(type, for_all) \
|
|
inline void from_json(const nlohmann::json &j, type &dest) { \
|
|
bts::io::JsonReader jr{&j}; \
|
|
for_all(_JSON_IMPL_READ_JSON) \
|
|
}
|
|
|
|
#define _JSON_OPTIONAL_IMPL_READ_JSON(field) \
|
|
jr.read_optional(#field, dest.field);
|
|
#define FROM_JSON_OPTIONAL_IMPL(type, for_all) \
|
|
inline void from_json(const nlohmann::json &j, type &dest) { \
|
|
bts::io::JsonReader jr{&j}; \
|
|
for_all(_JSON_OPTIONAL_IMPL_READ_JSON) \
|
|
}
|
|
|
|
#define JSON_IMPL(type, for_all) \
|
|
FROM_JSON_IMPL(type, for_all) \
|
|
TO_JSON_IMPL(type, for_all)
|
|
|
|
#define JSON_OPTIONAL_IMPL(type, for_all) \
|
|
FROM_JSON_OPTIONAL_IMPL(type, for_all) \
|
|
TO_JSON_OPTIONAL_IMPL(type, for_all)
|
|
|
|
#ifdef LOG_JSON_READ_ERRORS
|
|
#define JSON_READ_AS(json, field, type, dest) \
|
|
type dest; \
|
|
bts::system::json::JsonReader{&json}.read(#field, dest)
|
|
#else
|
|
#define JSON_READ_AS(j, field, type, dest) \
|
|
bts::io::JsonReader{&j}.read(#field, dest);
|
|
|
|
#define JSON_READ_OPTIONAL_AS(j, temp_var, field, type, dest) \
|
|
{ \
|
|
type temp_var{}; \
|
|
if (bts::io::JsonReader{&j}.read(#field, temp_var)) { \
|
|
dest = std::move(temp_var); \
|
|
} \
|
|
}
|
|
#endif
|
|
|
|
#define JSON_READ(json, dest, field) \
|
|
JSON_READ_AS(json, field, decltype(dest.field), dest.field)
|
|
#define JSON_READ_FIELDS(json, dest, fields) fields(json, dest, JSON_READ)
|
|
|
|
#define JSON_READ_OPTIONAL(js, dest, field) \
|
|
JSON_READ_OPTIONAL_AS( \
|
|
js, temp_var, field, \
|
|
std::remove_reference<decltype(dest.field.value())>::type, dest.field)
|
|
|
|
#define JSON_READ_OPTIONAL_FIELDS(json, dest, fields) \
|
|
fields(json, dest, JSON_READ_OPTIONAL)
|
|
|
|
#define JSON_WRITE(j, src, field) j[#field] = src.field;
|
|
#define JSON_WRITE_FIELDS(j, src, fields) fields(j, src, JSON_WRITE)
|
|
|
|
#define JSON_WRITE_OPTIONAL(j, src, field) \
|
|
if (src.field) { \
|
|
j[#field] = *src.field; \
|
|
}
|
|
#define JSON_WRITE_OPTIONAL_FIELDS(j, src, fields) \
|
|
fields(j, src, JSON_WRITE_OPTIONAL)
|
|
|
|
#define JSON_WRITE_OPTIONAL_AS(j, field, dest) \
|
|
if (dest) { \
|
|
j[#field] = *dest; \
|
|
}
|