2022-09-12 17:37:25 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "json.hpp"
|
|
|
|
#include "../loader/Log.hpp"
|
|
|
|
#include <variant>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
namespace geode {
|
2022-09-24 11:46:47 -04:00
|
|
|
template<class Json>
|
2022-09-12 17:37:25 -04:00
|
|
|
struct JsonChecker;
|
|
|
|
|
2022-09-26 06:53:40 -04:00
|
|
|
template <typename T, typename = void>
|
|
|
|
struct is_iterable : std::false_type {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct is_iterable<T,
|
|
|
|
std::void_t<
|
|
|
|
decltype(std::begin(std::declval<T>())),
|
|
|
|
decltype(std::end(std::declval<T>()))
|
|
|
|
>
|
|
|
|
> : std::true_type {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool is_iterable_v = is_iterable<T>::value;
|
|
|
|
|
2022-09-12 17:37:25 -04:00
|
|
|
namespace {
|
|
|
|
using value_t = nlohmann::detail::value_t;
|
|
|
|
|
|
|
|
constexpr const char* jsonValueTypeToString(value_t type) {
|
|
|
|
switch (type) {
|
|
|
|
default:
|
|
|
|
case value_t::null: return "null";
|
|
|
|
case value_t::object: return "object";
|
|
|
|
case value_t::array: return "array";
|
|
|
|
case value_t::string: return "string";
|
|
|
|
case value_t::boolean: return "boolean";
|
|
|
|
case value_t::binary: return "binary";
|
|
|
|
case value_t::discarded: return "discarded";
|
|
|
|
case value_t::number_integer: return "integer";
|
|
|
|
case value_t::number_unsigned: return "integer";
|
|
|
|
case value_t::number_float: return "number";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
constexpr value_t getJsonType() {
|
|
|
|
if constexpr (std::is_same_v<T, bool>) {
|
|
|
|
return value_t::boolean;
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_floating_point_v<T>) {
|
|
|
|
return value_t::number_float;
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_unsigned_v<T>) {
|
|
|
|
return value_t::number_unsigned;
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_integral_v<T>) {
|
|
|
|
return value_t::number_integer;
|
|
|
|
}
|
|
|
|
else if constexpr (
|
2022-09-26 06:53:40 -04:00
|
|
|
std::is_constructible_v<T, std::string>
|
2022-09-12 17:37:25 -04:00
|
|
|
) {
|
|
|
|
return value_t::string;
|
|
|
|
}
|
2022-09-26 06:53:40 -04:00
|
|
|
else if constexpr (is_iterable_v<T>) {
|
|
|
|
return value_t::array;
|
|
|
|
}
|
2022-09-12 17:37:25 -04:00
|
|
|
return value_t::null;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool jsonConvertibleTo(value_t value, value_t to) {
|
2022-09-21 07:50:23 -04:00
|
|
|
// if we don't know the type we're passing into,
|
|
|
|
// everything's valid
|
|
|
|
if (to == value_t::null) return true;
|
2022-09-12 17:37:25 -04:00
|
|
|
if (
|
|
|
|
value == value_t::number_float ||
|
|
|
|
value == value_t::number_integer ||
|
|
|
|
value == value_t::number_unsigned
|
|
|
|
) {
|
|
|
|
return
|
|
|
|
to == value_t::number_float ||
|
|
|
|
to == value_t::number_integer ||
|
|
|
|
to == value_t::number_unsigned;
|
|
|
|
}
|
|
|
|
return value == to;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
using JsonValueValidator = bool(*)(T const&);
|
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
template<class Json>
|
2022-09-12 17:37:25 -04:00
|
|
|
struct JsonMaybeObject;
|
2022-09-24 11:46:47 -04:00
|
|
|
template<class Json>
|
|
|
|
struct JsonMaybeValue;
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
template<class Json>
|
|
|
|
struct JsonMaybeSomething {
|
2022-09-12 17:37:25 -04:00
|
|
|
protected:
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonChecker<Json>& m_checker;
|
|
|
|
Json& m_json;
|
2022-09-12 17:37:25 -04:00
|
|
|
std::string m_hierarchy;
|
|
|
|
bool m_hasValue;
|
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
friend struct JsonMaybeObject<Json>;
|
|
|
|
friend struct JsonMaybeValue<Json>;
|
|
|
|
|
2022-09-12 17:37:25 -04:00
|
|
|
void setError(std::string const& error);
|
|
|
|
|
|
|
|
public:
|
2022-09-24 11:46:47 -04:00
|
|
|
Json& json() {
|
2022-09-12 17:37:25 -04:00
|
|
|
return m_json;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonMaybeSomething(
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonChecker<Json>& checker,
|
|
|
|
Json& json,
|
2022-09-12 17:37:25 -04:00
|
|
|
std::string const& hierarchy,
|
|
|
|
bool hasValue
|
|
|
|
) : m_checker(checker),
|
|
|
|
m_json(json),
|
|
|
|
m_hierarchy(hierarchy),
|
|
|
|
m_hasValue(hasValue) {}
|
|
|
|
|
|
|
|
bool isError() const;
|
|
|
|
|
|
|
|
operator bool() const {
|
|
|
|
return !isError();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
template<class Json>
|
|
|
|
struct JsonMaybeValue : public JsonMaybeSomething<Json> {
|
2022-09-12 17:37:25 -04:00
|
|
|
bool m_inferType = true;
|
|
|
|
|
|
|
|
JsonMaybeValue(
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonChecker<Json>& checker,
|
|
|
|
Json& json,
|
2022-09-12 17:37:25 -04:00
|
|
|
std::string const& hierarchy,
|
|
|
|
bool hasValue
|
2022-09-24 11:46:47 -04:00
|
|
|
) : JsonMaybeSomething<Json>(checker, json, hierarchy, hasValue) {}
|
|
|
|
|
|
|
|
JsonMaybeSomething<Json>& self() {
|
|
|
|
return *static_cast<JsonMaybeSomething<Json>*>(this);
|
|
|
|
}
|
2022-09-12 17:37:25 -04:00
|
|
|
|
|
|
|
template<nlohmann::detail::value_t T>
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonMaybeValue<Json> as() {
|
2022-09-12 17:37:25 -04:00
|
|
|
if (this->isError()) return *this;
|
2022-09-24 11:46:47 -04:00
|
|
|
if (!jsonConvertibleTo(self().m_json.type(), T)) {
|
2022-09-12 17:37:25 -04:00
|
|
|
this->setError(
|
2022-09-24 11:46:47 -04:00
|
|
|
self().m_hierarchy + ": Invalid type \"" +
|
|
|
|
self().m_json.type_name() + "\", expected \"" +
|
2022-09-12 17:37:25 -04:00
|
|
|
jsonValueTypeToString(T) + "\""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
m_inferType = false;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<nlohmann::detail::value_t... T>
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonMaybeValue<Json> asOneOf() {
|
2022-09-12 17:37:25 -04:00
|
|
|
if (this->isError()) return *this;
|
2022-09-24 11:46:47 -04:00
|
|
|
bool isOneOf = (... || jsonConvertibleTo(self().m_json.type(), T));
|
2022-09-12 17:37:25 -04:00
|
|
|
if (!isOneOf) {
|
|
|
|
this->setError(
|
2022-09-24 11:46:47 -04:00
|
|
|
self().m_hierarchy + ": Invalid type \"" +
|
|
|
|
self().m_json.type_name() + "\", expected one of \"" +
|
2022-09-12 17:37:25 -04:00
|
|
|
(jsonValueTypeToString(T), ...) + "\""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
m_inferType = false;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<nlohmann::detail::value_t T>
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonMaybeValue<Json> is() {
|
2022-09-12 17:37:25 -04:00
|
|
|
if (this->isError()) return *this;
|
2022-09-24 11:46:47 -04:00
|
|
|
self().m_hasValue = jsonConvertibleTo(self().m_json.type(), T);
|
2022-09-12 17:37:25 -04:00
|
|
|
m_inferType = false;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonMaybeValue<Json> validate(JsonValueValidator<T> validator) {
|
2022-09-12 17:37:25 -04:00
|
|
|
if (this->isError()) return *this;
|
|
|
|
try {
|
2022-09-28 09:21:05 -04:00
|
|
|
if (!validator(self().m_json.template get<T>())) {
|
2022-09-24 11:46:47 -04:00
|
|
|
this->setError(self().m_hierarchy + ": Invalid value format");
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
|
|
|
} catch(...) {
|
|
|
|
this->setError(
|
2022-09-24 11:46:47 -04:00
|
|
|
self().m_hierarchy + ": Invalid type \"" +
|
|
|
|
std::string(self().m_json.type_name()) + "\""
|
2022-09-12 17:37:25 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonMaybeValue<Json> inferType() {
|
2022-09-12 17:37:25 -04:00
|
|
|
if (this->isError() || !m_inferType) return *this;
|
|
|
|
return this->as<getJsonType<T>()>();
|
|
|
|
}
|
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
template<class T>
|
|
|
|
JsonMaybeValue<Json> intoRaw(T& target) {
|
2022-09-12 17:37:25 -04:00
|
|
|
if (this->isError()) return *this;
|
2022-09-24 11:46:47 -04:00
|
|
|
target = self().m_json;
|
2022-09-12 17:37:25 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonMaybeValue<Json> into(T& target) {
|
2022-09-12 17:37:25 -04:00
|
|
|
return this->intoAs<T, T>(target);
|
|
|
|
}
|
|
|
|
|
2022-09-29 15:34:28 -04:00
|
|
|
template<class T>
|
|
|
|
JsonMaybeValue<Json> into(std::optional<T>& target) {
|
|
|
|
return this->intoAs<T, std::optional<T>>(target);
|
|
|
|
}
|
|
|
|
|
2022-09-12 17:37:25 -04:00
|
|
|
template<class A, class T>
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonMaybeValue<Json> intoAs(T& target) {
|
2022-09-12 17:37:25 -04:00
|
|
|
this->inferType<A>();
|
|
|
|
if (this->isError()) return *this;
|
|
|
|
try {
|
2022-09-28 09:21:05 -04:00
|
|
|
target = self().m_json.template get<A>();
|
2022-09-12 17:37:25 -04:00
|
|
|
} catch(...) {
|
|
|
|
this->setError(
|
2022-09-24 11:46:47 -04:00
|
|
|
self().m_hierarchy + ": Invalid type \"" +
|
|
|
|
std::string(self().m_json.type_name()) + "\""
|
2022-09-12 17:37:25 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T get() {
|
|
|
|
this->inferType<T>();
|
|
|
|
if (this->isError()) return T();
|
|
|
|
try {
|
2022-09-28 09:21:05 -04:00
|
|
|
return self().m_json.template get<T>();
|
2022-09-12 17:37:25 -04:00
|
|
|
} catch(...) {
|
|
|
|
this->setError(
|
2022-09-24 11:46:47 -04:00
|
|
|
self().m_hierarchy + ": Invalid type to get \"" +
|
|
|
|
std::string(self().m_json.type_name()) + "\""
|
2022-09-12 17:37:25 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return T();
|
|
|
|
}
|
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonMaybeObject<Json> obj();
|
2022-09-12 17:37:25 -04:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct Iterator {
|
|
|
|
std::vector<T> m_values;
|
|
|
|
|
2022-09-19 16:03:50 -04:00
|
|
|
using iterator = typename std::vector<T>::iterator;
|
|
|
|
using const_iterator = typename std::vector<T>::const_iterator;
|
2022-09-12 17:37:25 -04:00
|
|
|
|
|
|
|
iterator begin() {
|
|
|
|
return m_values.begin();
|
|
|
|
}
|
|
|
|
iterator end() {
|
|
|
|
return m_values.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator begin() const {
|
|
|
|
return m_values.begin();
|
|
|
|
}
|
|
|
|
const_iterator end() const {
|
|
|
|
return m_values.end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
Iterator<JsonMaybeValue<Json>> iterate() {
|
2022-09-12 17:37:25 -04:00
|
|
|
this->as<value_t::array>();
|
2022-09-24 11:46:47 -04:00
|
|
|
Iterator<JsonMaybeValue<Json>> iter;
|
2022-09-12 17:37:25 -04:00
|
|
|
if (this->isError()) return iter;
|
|
|
|
size_t i = 0;
|
2022-09-24 11:46:47 -04:00
|
|
|
for (auto& obj : self().m_json) {
|
2022-09-12 17:37:25 -04:00
|
|
|
iter.m_values.emplace_back(
|
2022-09-24 11:46:47 -04:00
|
|
|
self().m_checker, obj,
|
|
|
|
self().m_hierarchy + "." + std::to_string(i++),
|
|
|
|
self().m_hasValue
|
2022-09-12 17:37:25 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
Iterator<std::pair<std::string, JsonMaybeValue<Json>>> items() {
|
2022-09-12 17:37:25 -04:00
|
|
|
this->as<value_t::object>();
|
2022-09-24 11:46:47 -04:00
|
|
|
Iterator<std::pair<std::string, JsonMaybeValue<Json>>> iter;
|
2022-09-12 17:37:25 -04:00
|
|
|
if (this->isError()) return iter;
|
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
for (auto& [k, v] : self().m_json.items()) {
|
|
|
|
iter.m_values.emplace_back(k, JsonMaybeValue<Json>(
|
|
|
|
self().m_checker, v,
|
|
|
|
self().m_hierarchy + "." + k,
|
|
|
|
self().m_hasValue
|
2022-09-12 17:37:25 -04:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
template<class Json>
|
|
|
|
struct JsonMaybeObject : JsonMaybeSomething<Json> {
|
2022-09-12 17:37:25 -04:00
|
|
|
std::set<std::string> m_knownKeys;
|
|
|
|
|
|
|
|
JsonMaybeObject(
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonChecker<Json>& checker,
|
|
|
|
Json& json,
|
2022-09-12 17:37:25 -04:00
|
|
|
std::string const& hierarchy,
|
|
|
|
bool hasValue
|
2022-09-24 11:46:47 -04:00
|
|
|
) : JsonMaybeSomething<Json>(checker, json, hierarchy, hasValue) {}
|
|
|
|
|
|
|
|
JsonMaybeSomething<Json>& self() {
|
|
|
|
return *static_cast<JsonMaybeSomething<Json>*>(this);
|
|
|
|
}
|
2022-09-12 17:37:25 -04:00
|
|
|
|
|
|
|
void addKnownKey(std::string const& key) {
|
|
|
|
m_knownKeys.insert(key);
|
|
|
|
}
|
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
Json& json() {
|
|
|
|
return self().m_json;
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonMaybeValue<Json> emptyValue() {
|
|
|
|
return JsonMaybeValue(self().m_checker, self().m_json, "", false);
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonMaybeValue<Json> has(std::string const& key) {
|
2022-09-12 17:37:25 -04:00
|
|
|
this->addKnownKey(key);
|
2022-09-17 08:42:33 -04:00
|
|
|
if (this->isError()) return emptyValue();
|
2022-09-24 11:46:47 -04:00
|
|
|
if (!self().m_json.contains(key) || self().m_json[key].is_null()) {
|
2022-09-12 17:37:25 -04:00
|
|
|
return emptyValue();
|
|
|
|
}
|
2022-09-24 11:46:47 -04:00
|
|
|
return JsonMaybeValue<Json>(self().m_checker, self().m_json[key], key, true);
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonMaybeValue<Json> needs(std::string const& key) {
|
2022-09-12 17:37:25 -04:00
|
|
|
this->addKnownKey(key);
|
2022-09-17 08:42:33 -04:00
|
|
|
if (this->isError()) return emptyValue();
|
2022-09-24 11:46:47 -04:00
|
|
|
if (!self().m_json.contains(key)) {
|
2022-09-12 17:37:25 -04:00
|
|
|
this->setError(
|
2022-09-24 11:46:47 -04:00
|
|
|
self().m_hierarchy + " is missing required key \"" + key + "\""
|
2022-09-12 17:37:25 -04:00
|
|
|
);
|
|
|
|
return emptyValue();
|
|
|
|
}
|
2022-09-24 11:46:47 -04:00
|
|
|
return JsonMaybeValue<Json>(self().m_checker, self().m_json[key], key, true);
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void checkUnknownKeys() {
|
2022-09-24 11:46:47 -04:00
|
|
|
for (auto& [key, _] : self().m_json.items()) {
|
2022-09-12 17:37:25 -04:00
|
|
|
if (!m_knownKeys.count(key)) {
|
2022-09-24 11:46:47 -04:00
|
|
|
Log::get() << self().m_hierarchy + " contains unknown key \"" + key + "\"";
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-26 15:29:36 -04:00
|
|
|
template<class Json = nlohmann::json>
|
2022-09-24 11:46:47 -04:00
|
|
|
struct JsonChecker {
|
2022-09-12 17:37:25 -04:00
|
|
|
std::variant<std::monostate, std::string> m_result;
|
2022-09-24 11:46:47 -04:00
|
|
|
Json& m_json;
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonChecker(Json& json) : m_json(json), m_result(std::monostate()) {}
|
2022-09-12 17:37:25 -04:00
|
|
|
|
|
|
|
bool isError() const {
|
|
|
|
return std::holds_alternative<std::string>(m_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getError() const {
|
|
|
|
return std::get<std::string>(m_result);
|
|
|
|
}
|
|
|
|
|
2022-09-24 11:46:47 -04:00
|
|
|
JsonMaybeValue<Json> root(std::string const& hierarchy) {
|
2022-09-17 08:42:33 -04:00
|
|
|
return JsonMaybeValue(*this, m_json, hierarchy, true);
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
|
|
|
};
|
2022-09-24 11:46:47 -04:00
|
|
|
|
|
|
|
template<class Json>
|
|
|
|
void JsonMaybeSomething<Json>::setError(std::string const& error) {
|
|
|
|
m_checker.m_result = error;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Json>
|
|
|
|
bool JsonMaybeSomething<Json>::isError() const {
|
|
|
|
return m_checker.isError() || !m_hasValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Json>
|
|
|
|
JsonMaybeObject<Json> JsonMaybeValue<Json>::obj() {
|
|
|
|
this->as<value_t::object>();
|
|
|
|
return JsonMaybeObject(
|
|
|
|
self().m_checker, self().m_json,
|
|
|
|
self().m_hierarchy, self().m_hasValue
|
|
|
|
);
|
|
|
|
}
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|