2022-09-12 17:37:25 -04:00
|
|
|
#pragma once
|
|
|
|
|
2023-12-30 12:42:53 -05:00
|
|
|
#include <matjson.hpp>
|
2022-12-04 11:39:40 -05:00
|
|
|
#include "../loader/Log.hpp"
|
2022-09-12 17:37:25 -04:00
|
|
|
#include <set>
|
2022-10-30 14:59:20 -04:00
|
|
|
#include <variant>
|
2024-02-11 13:58:46 -05:00
|
|
|
#include <Geode/utils/MiniFunction.hpp>
|
2024-10-02 18:51:06 -04:00
|
|
|
#include <Geode/utils/Result.hpp>
|
2022-09-12 17:37:25 -04:00
|
|
|
|
|
|
|
namespace geode {
|
|
|
|
struct JsonChecker;
|
|
|
|
|
2022-09-26 06:53:40 -04:00
|
|
|
template <typename T, typename = void>
|
|
|
|
struct is_iterable : std::false_type {};
|
|
|
|
|
|
|
|
template <typename T>
|
2022-10-30 14:59:20 -04:00
|
|
|
struct is_iterable<
|
2022-12-04 11:39:40 -05:00
|
|
|
T, std::void_t<decltype(std::begin(std::declval<T>())), decltype(std::end(std::declval<T>()))>> :
|
2022-10-30 14:59:20 -04:00
|
|
|
std::true_type {};
|
2022-09-26 06:53:40 -04:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool is_iterable_v = is_iterable<T>::value;
|
|
|
|
|
2022-09-12 17:37:25 -04:00
|
|
|
namespace {
|
2023-12-30 12:42:53 -05:00
|
|
|
using value_t = matjson::Type;
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
constexpr char const* jsonValueTypeToString(value_t type) {
|
2022-09-12 17:37:25 -04:00
|
|
|
switch (type) {
|
|
|
|
default:
|
2023-01-27 18:25:19 -05:00
|
|
|
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::Bool: return "boolean";
|
|
|
|
case value_t::Number: return "number";
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2022-09-12 17:37:25 -04:00
|
|
|
constexpr value_t getJsonType() {
|
|
|
|
if constexpr (std::is_same_v<T, bool>) {
|
2023-01-27 18:25:19 -05:00
|
|
|
return value_t::Bool;
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
|
|
|
else if constexpr (std::is_floating_point_v<T>) {
|
2023-01-27 18:25:19 -05:00
|
|
|
return value_t::Number;
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
|
|
|
else if constexpr (std::is_unsigned_v<T>) {
|
2023-01-27 18:25:19 -05:00
|
|
|
return value_t::Number;
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
|
|
|
else if constexpr (std::is_integral_v<T>) {
|
2023-01-27 18:25:19 -05:00
|
|
|
return value_t::Number;
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
else if constexpr (std::is_constructible_v<T, std::string>) {
|
2023-01-27 18:25:19 -05:00
|
|
|
return value_t::String;
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
2022-09-26 06:53:40 -04:00
|
|
|
else if constexpr (is_iterable_v<T>) {
|
2023-01-27 18:25:19 -05:00
|
|
|
return value_t::Array;
|
2022-09-26 06:53:40 -04:00
|
|
|
}
|
2023-01-27 18:25:19 -05:00
|
|
|
return value_t::Null;
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool jsonConvertibleTo(value_t value, value_t to) {
|
2022-10-30 14:59:20 -04:00
|
|
|
// if we don't know the type we're passing into,
|
2022-09-21 07:50:23 -04:00
|
|
|
// everything's valid
|
2023-01-27 18:25:19 -05:00
|
|
|
if (to == value_t::Null) return true;
|
|
|
|
|
|
|
|
if (value == value_t::Number) {
|
|
|
|
return to == value_t::Number;
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
2023-01-27 18:25:19 -05:00
|
|
|
|
2022-09-12 17:37:25 -04:00
|
|
|
return value == to;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2023-02-08 10:25:07 -05:00
|
|
|
using JsonValueValidator = utils::MiniFunction<bool(T const&)>;
|
2022-09-12 17:37:25 -04:00
|
|
|
|
|
|
|
struct JsonMaybeObject;
|
2022-09-24 11:46:47 -04:00
|
|
|
struct JsonMaybeValue;
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2024-08-13 06:34:33 -04:00
|
|
|
struct GEODE_DLL
|
|
|
|
[[deprecated("Use JsonExpectedValue via the checkJson function instead")]]
|
|
|
|
JsonMaybeSomething {
|
2022-09-12 17:37:25 -04:00
|
|
|
protected:
|
2023-02-08 10:25:07 -05:00
|
|
|
JsonChecker& m_checker;
|
2023-12-30 12:42:53 -05:00
|
|
|
matjson::Value& m_json;
|
2022-09-12 17:37:25 -04:00
|
|
|
std::string m_hierarchy;
|
|
|
|
bool m_hasValue;
|
|
|
|
|
2023-02-08 10:25:07 -05:00
|
|
|
friend struct JsonMaybeObject;
|
|
|
|
friend struct JsonMaybeValue;
|
2022-09-24 11:46:47 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
void setError(std::string const& error);
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-09-12 17:37:25 -04:00
|
|
|
public:
|
2023-12-30 12:42:53 -05:00
|
|
|
matjson::Value& json();
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
JsonMaybeSomething(
|
2023-12-30 12:42:53 -05:00
|
|
|
JsonChecker& checker, matjson::Value& json, std::string const& hierarchy, bool hasValue
|
2022-10-23 09:22:27 -04:00
|
|
|
);
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
bool isError() const;
|
|
|
|
std::string getError() const;
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
operator bool() const;
|
2022-09-12 17:37:25 -04:00
|
|
|
};
|
|
|
|
|
2024-08-13 06:34:33 -04:00
|
|
|
struct GEODE_DLL
|
|
|
|
[[deprecated("Use JsonExpectedValue via the checkJson function instead")]]
|
|
|
|
JsonMaybeValue : public JsonMaybeSomething {
|
2022-09-12 17:37:25 -04:00
|
|
|
bool m_inferType = true;
|
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
JsonMaybeValue(
|
2023-12-30 12:42:53 -05:00
|
|
|
JsonChecker& checker, matjson::Value& json, std::string const& hierarchy, bool hasValue
|
2022-10-23 09:22:27 -04:00
|
|
|
);
|
2022-09-24 11:46:47 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
JsonMaybeSomething& self();
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-12-30 12:42:53 -05:00
|
|
|
template <matjson::Type T>
|
2023-02-08 10:25:07 -05:00
|
|
|
JsonMaybeValue& 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(
|
2023-01-27 18:25:19 -05:00
|
|
|
self().m_hierarchy + ": Invalid type \"" + jsonValueTypeToString(self().m_json.type()) +
|
2022-10-30 14:59:20 -04:00
|
|
|
"\", expected \"" + jsonValueTypeToString(T) + "\""
|
2022-09-12 17:37:25 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
m_inferType = false;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
JsonMaybeValue& array();
|
2022-10-12 17:22:43 -04:00
|
|
|
|
2023-12-30 12:42:53 -05:00
|
|
|
template <matjson::Type... T>
|
2023-02-08 10:25:07 -05:00
|
|
|
JsonMaybeValue& 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(
|
2023-01-27 18:25:19 -05:00
|
|
|
self().m_hierarchy + ": Invalid type \"" + jsonValueTypeToString(self().m_json.type()) +
|
2022-10-30 14:59:20 -04:00
|
|
|
"\", expected one of \"" + (jsonValueTypeToString(T), ...) + "\""
|
2022-09-12 17:37:25 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
m_inferType = false;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-02-10 06:02:31 -05:00
|
|
|
template <class T>
|
|
|
|
bool is() {
|
|
|
|
if (this->isError()) return false;
|
|
|
|
return self().m_json.template is<T>();
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2023-02-08 10:25:07 -05:00
|
|
|
JsonMaybeValue& validate(JsonValueValidator<T> validator) {
|
2022-09-12 17:37:25 -04:00
|
|
|
if (this->isError()) return *this;
|
2024-01-24 09:17:42 -05:00
|
|
|
if (self().m_json.template is<T>()) {
|
2023-01-27 18:25:19 -05:00
|
|
|
if (!validator(self().m_json.template as<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
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
2024-01-24 09:17:42 -05:00
|
|
|
else {
|
2022-09-12 17:37:25 -04:00
|
|
|
this->setError(
|
2022-09-24 11:46:47 -04:00
|
|
|
self().m_hierarchy + ": Invalid type \"" +
|
2023-01-27 18:25:19 -05:00
|
|
|
std::string(jsonValueTypeToString(self().m_json.type())) + "\""
|
2022-09-12 17:37:25 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-04 11:39:40 -05:00
|
|
|
template <class T>
|
2023-02-08 10:25:07 -05:00
|
|
|
JsonMaybeValue& inferType() {
|
2022-09-12 17:37:25 -04:00
|
|
|
if (this->isError() || !m_inferType) return *this;
|
|
|
|
return this->as<getJsonType<T>()>();
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2023-02-08 10:25:07 -05:00
|
|
|
JsonMaybeValue& 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;
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2023-02-08 10:25:07 -05:00
|
|
|
JsonMaybeValue& into(T& target) {
|
2022-09-12 17:37:25 -04:00
|
|
|
return this->intoAs<T, T>(target);
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2023-02-08 10:25:07 -05:00
|
|
|
JsonMaybeValue& into(std::optional<T>& target) {
|
2022-09-29 15:34:28 -04:00
|
|
|
return this->intoAs<T, std::optional<T>>(target);
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class A, class T>
|
2023-02-08 10:25:07 -05:00
|
|
|
JsonMaybeValue& intoAs(T& target) {
|
2022-09-12 17:37:25 -04:00
|
|
|
this->inferType<A>();
|
|
|
|
if (this->isError()) return *this;
|
2023-01-27 18:25:19 -05:00
|
|
|
|
2024-01-24 09:17:42 -05:00
|
|
|
if (self().m_json.template is<A>()) {
|
2024-02-12 10:37:50 -05:00
|
|
|
try {
|
|
|
|
target = self().m_json.template as<A>();
|
|
|
|
}
|
|
|
|
catch(matjson::JsonException const& e) {
|
|
|
|
this->setError(
|
|
|
|
self().m_hierarchy + ": Error parsing JSON: " + std::string(e.what())
|
|
|
|
);
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
2024-01-24 09:17:42 -05:00
|
|
|
else {
|
2022-09-12 17:37:25 -04:00
|
|
|
this->setError(
|
2022-10-30 14:59:20 -04:00
|
|
|
self().m_hierarchy + ": Invalid type \"" +
|
2024-01-24 09:17:42 -05:00
|
|
|
std::string(jsonValueTypeToString(self().m_json.type())) + "\""
|
2022-09-12 17:37:25 -04:00
|
|
|
);
|
|
|
|
}
|
2023-01-27 18:25:19 -05:00
|
|
|
|
2022-09-12 17:37:25 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2022-09-12 17:37:25 -04:00
|
|
|
T get() {
|
|
|
|
this->inferType<T>();
|
|
|
|
if (this->isError()) return T();
|
2024-01-24 09:17:42 -05:00
|
|
|
if (self().m_json.template is<T>()) {
|
|
|
|
return self().m_json.template as<T>();
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|
|
|
|
return T();
|
|
|
|
}
|
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
JsonMaybeObject obj();
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2022-09-12 17:37:25 -04:00
|
|
|
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();
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-09-12 17:37:25 -04:00
|
|
|
iterator end() {
|
|
|
|
return m_values.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator begin() const {
|
|
|
|
return m_values.begin();
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-09-12 17:37:25 -04:00
|
|
|
const_iterator end() const {
|
|
|
|
return m_values.end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
JsonMaybeValue at(size_t i);
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
Iterator<JsonMaybeValue> iterate();
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
Iterator<std::pair<std::string, JsonMaybeValue>> items();
|
2022-09-12 17:37:25 -04:00
|
|
|
};
|
|
|
|
|
2024-08-13 06:34:33 -04:00
|
|
|
struct
|
|
|
|
[[deprecated("Use JsonExpectedValue via the checkJson function instead")]]
|
|
|
|
GEODE_DLL JsonMaybeObject : JsonMaybeSomething {
|
2022-09-12 17:37:25 -04:00
|
|
|
std::set<std::string> m_knownKeys;
|
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
JsonMaybeObject(
|
2023-12-30 12:42:53 -05:00
|
|
|
JsonChecker& checker, matjson::Value& json, std::string const& hierarchy, bool hasValue
|
2022-10-23 09:22:27 -04:00
|
|
|
);
|
2022-09-24 11:46:47 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
JsonMaybeSomething& self();
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
void addKnownKey(std::string const& key);
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-12-30 12:42:53 -05:00
|
|
|
matjson::Value& json();
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
JsonMaybeValue emptyValue();
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
JsonMaybeValue has(std::string const& key);
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
JsonMaybeValue needs(std::string const& key);
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
void checkUnknownKeys();
|
2022-09-12 17:37:25 -04:00
|
|
|
};
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2024-08-13 06:34:33 -04:00
|
|
|
struct
|
|
|
|
[[deprecated("Use JsonExpectedValue via the checkJson function instead")]]
|
|
|
|
GEODE_DLL JsonChecker {
|
2022-09-12 17:37:25 -04:00
|
|
|
std::variant<std::monostate, std::string> m_result;
|
2023-12-30 12:42:53 -05:00
|
|
|
matjson::Value& m_json;
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-12-30 12:42:53 -05:00
|
|
|
JsonChecker(matjson::Value& json);
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
bool isError() const;
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
std::string getError() const;
|
2022-09-12 17:37:25 -04:00
|
|
|
|
2023-02-10 10:09:05 -05:00
|
|
|
JsonMaybeValue root(std::string const& hierarchy);
|
2022-09-12 17:37:25 -04:00
|
|
|
};
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2024-08-13 06:34:33 -04:00
|
|
|
class GEODE_DLL JsonExpectedValue final {
|
|
|
|
protected:
|
|
|
|
class Impl;
|
|
|
|
std::unique_ptr<Impl> m_impl;
|
|
|
|
|
|
|
|
JsonExpectedValue();
|
|
|
|
JsonExpectedValue(Impl* from, matjson::Value& scope, std::string_view key);
|
|
|
|
|
2024-08-23 16:35:29 -04:00
|
|
|
static const char* matJsonTypeToString(matjson::Type ty);
|
|
|
|
|
2024-08-13 06:34:33 -04:00
|
|
|
bool hasError() const;
|
|
|
|
void setError(std::string_view error);
|
|
|
|
|
|
|
|
matjson::Value const& getJSONRef() const;
|
|
|
|
|
|
|
|
template <class... Args>
|
|
|
|
void setError(fmt::format_string<Args...> error, Args&&... args) {
|
|
|
|
this->setError(fmt::format(error, std::forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
std::optional<T> tryGet() {
|
|
|
|
if (this->hasError()) return std::nullopt;
|
2024-08-21 14:41:44 -04:00
|
|
|
if constexpr (std::is_same_v<T, matjson::Value>) {
|
|
|
|
return this->getJSONRef();
|
2024-08-13 06:34:33 -04:00
|
|
|
}
|
2024-08-21 14:41:44 -04:00
|
|
|
else {
|
|
|
|
try {
|
2024-08-23 16:35:29 -04:00
|
|
|
if (this->getJSONRef().template is<T>()) {
|
|
|
|
return this->getJSONRef().template as<T>();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->setError(
|
|
|
|
"unexpected type {}",
|
|
|
|
this->matJsonTypeToString(this->getJSONRef().type())
|
|
|
|
);
|
|
|
|
}
|
2024-08-21 14:41:44 -04:00
|
|
|
}
|
|
|
|
// matjson can throw variant exceptions too so you need to do this
|
|
|
|
catch(std::exception const& e) {
|
2024-08-23 16:35:29 -04:00
|
|
|
this->setError("unable to parse json: {}", e);
|
2024-08-21 14:41:44 -04:00
|
|
|
}
|
2024-08-13 06:34:33 -04:00
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
JsonExpectedValue(matjson::Value const& value, std::string_view rootScopeName);
|
|
|
|
~JsonExpectedValue();
|
|
|
|
|
|
|
|
JsonExpectedValue(JsonExpectedValue&&);
|
|
|
|
JsonExpectedValue& operator=(JsonExpectedValue&&);
|
|
|
|
JsonExpectedValue(JsonExpectedValue const&) = delete;
|
|
|
|
JsonExpectedValue& operator=(JsonExpectedValue const&) = delete;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a copy of the underlying raw JSON value
|
|
|
|
*/
|
|
|
|
matjson::Value json() const;
|
|
|
|
/**
|
|
|
|
* Get the key name of this JSON value. If this is an array index,
|
|
|
|
* returns the index as a string. If this is the root object,
|
|
|
|
* returns the root scope name.
|
|
|
|
*/
|
|
|
|
std::string key() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the type of this JSON value. Does not set an error. If an
|
|
|
|
* error is already set, always returns false
|
|
|
|
*/
|
|
|
|
bool is(matjson::Type type) const;
|
|
|
|
bool isNull() const;
|
|
|
|
bool isBool() const;
|
|
|
|
bool isNumber() const;
|
|
|
|
bool isString() const;
|
|
|
|
bool isArray() const;
|
|
|
|
bool isObject() const;
|
|
|
|
/**
|
|
|
|
* Asserts that this JSON value is of the specified type. If it is
|
|
|
|
* not, an error is set and all subsequent operations are no-ops
|
|
|
|
* @returns Itself
|
|
|
|
*/
|
|
|
|
JsonExpectedValue& assertIs(matjson::Type type);
|
|
|
|
JsonExpectedValue& assertIsNull();
|
|
|
|
JsonExpectedValue& assertIsBool();
|
|
|
|
JsonExpectedValue& assertIsNumber();
|
|
|
|
JsonExpectedValue& assertIsString();
|
|
|
|
JsonExpectedValue& assertIsArray();
|
|
|
|
JsonExpectedValue& assertIsObject();
|
|
|
|
/**
|
|
|
|
* Asserts that this JSON value is one of a list of specified types
|
|
|
|
* @returns Itself
|
|
|
|
*/
|
|
|
|
JsonExpectedValue& assertIs(std::initializer_list<matjson::Type> type);
|
|
|
|
|
|
|
|
// -- Dealing with values --
|
|
|
|
|
|
|
|
template <class T>
|
2024-08-23 15:56:17 -04:00
|
|
|
T get(T const& defaultValue = T()) {
|
2024-08-13 06:34:33 -04:00
|
|
|
if (auto v = this->template tryGet<T>()) {
|
|
|
|
return *std::move(v);
|
|
|
|
}
|
2024-08-23 15:56:17 -04:00
|
|
|
return defaultValue;
|
2024-08-13 06:34:33 -04:00
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
JsonExpectedValue& into(T& value) {
|
|
|
|
if (auto v = this->template tryGet<T>()) {
|
|
|
|
value = *std::move(v);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
JsonExpectedValue& into(std::optional<T>& value) {
|
|
|
|
if (auto v = this->template tryGet<T>()) {
|
|
|
|
value.emplace(*std::move(v));
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
JsonExpectedValue& mustBe(std::string_view name, auto predicate) requires requires {
|
|
|
|
{ predicate(std::declval<T>()) } -> std::convertible_to<bool>;
|
|
|
|
} {
|
|
|
|
if (this->hasError()) return *this;
|
|
|
|
if (auto v = this->template tryGet<T>()) {
|
2024-08-19 12:19:29 -04:00
|
|
|
if (!predicate(*v)) {
|
2024-08-13 06:34:33 -04:00
|
|
|
this->setError("json value is not {}", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2024-08-28 15:38:07 -04:00
|
|
|
template <class T>
|
|
|
|
JsonExpectedValue& mustBe(std::string_view name, auto predicate) requires requires {
|
|
|
|
{ predicate(std::declval<T>()) } -> std::convertible_to<Result<>>;
|
|
|
|
} {
|
|
|
|
if (this->hasError()) return *this;
|
|
|
|
if (auto v = this->template tryGet<T>()) {
|
|
|
|
auto p = predicate(*v);
|
|
|
|
if (!p) {
|
|
|
|
this->setError("json value is not {}: {}", name, p.unwrapErr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2024-08-13 06:34:33 -04:00
|
|
|
|
|
|
|
// -- Dealing with objects --
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this object has an optional key. Asserts that this JSON
|
|
|
|
* value is an object. If the key doesn't exist, returns a
|
|
|
|
* `JsonExpectValue` that does nothing
|
|
|
|
* @returns The key, which is a no-op value if it didn't exist
|
|
|
|
*/
|
|
|
|
JsonExpectedValue has(std::string_view key);
|
|
|
|
/**
|
|
|
|
* Check if this object has an optional key. Asserts that this JSON
|
|
|
|
* value is an object. If the key doesn't exist, sets an error and
|
|
|
|
* returns a `JsonExpectValue` that does nothing
|
|
|
|
* @returns The key, which is a no-op value if it didn't exist
|
|
|
|
*/
|
|
|
|
JsonExpectedValue needs(std::string_view key);
|
|
|
|
/**
|
|
|
|
* Asserts that this JSON value is an object. Get all object
|
|
|
|
* properties
|
|
|
|
*/
|
|
|
|
std::vector<std::pair<std::string, JsonExpectedValue>> properties();
|
|
|
|
/**
|
|
|
|
* Asserts that this JSON value is an object. Logs unknown keys to
|
|
|
|
* the console as warnings
|
|
|
|
*/
|
|
|
|
void checkUnknownKeys();
|
|
|
|
|
|
|
|
// -- Dealing with arrays --
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that this JSON value is an array. Returns the length of
|
|
|
|
* the array, or 0 on error
|
|
|
|
*/
|
|
|
|
size_t length();
|
|
|
|
/**
|
|
|
|
* Asserts that this JSON value is an array. Returns the value at
|
|
|
|
* the specified index. If there is no value at that index, sets an
|
|
|
|
* error
|
|
|
|
*/
|
|
|
|
JsonExpectedValue at(size_t index);
|
|
|
|
/**
|
|
|
|
* Asserts that this JSON value is an array. Returns the array items
|
|
|
|
* @warning The old JsonChecker used `items` for iterating object
|
|
|
|
* properties - on this new API that function is called `properties`!
|
|
|
|
*/
|
|
|
|
std::vector<JsonExpectedValue> items();
|
|
|
|
|
|
|
|
operator bool() const;
|
|
|
|
|
|
|
|
Result<> ok();
|
|
|
|
template <class T>
|
2024-08-19 12:19:29 -04:00
|
|
|
Result<T> ok(T value) {
|
2024-08-13 06:34:33 -04:00
|
|
|
auto ok = this->ok();
|
|
|
|
if (!ok) {
|
|
|
|
return Err(ok.unwrapErr());
|
|
|
|
}
|
2024-08-19 12:19:29 -04:00
|
|
|
return Ok(std::forward<T>(value));
|
2024-08-13 06:34:33 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
GEODE_DLL JsonExpectedValue checkJson(matjson::Value const& json, std::string_view rootScopeName);
|
2022-09-12 17:37:25 -04:00
|
|
|
}
|