2024-06-05 19:04:53 -04:00
|
|
|
#ifndef RFL_JSON_READER_HPP_
|
|
|
|
#define RFL_JSON_READER_HPP_
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <concepts>
|
|
|
|
#include <exception>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2024-06-08 14:10:59 -04:00
|
|
|
#include <yyjson.h>
|
2024-06-05 19:04:53 -04:00
|
|
|
|
|
|
|
#include "../Result.hpp"
|
|
|
|
#include "../always_false.hpp"
|
|
|
|
|
|
|
|
namespace rfl {
|
2024-06-08 14:10:59 -04:00
|
|
|
namespace json {
|
|
|
|
|
|
|
|
struct Reader {
|
|
|
|
struct YYJSONInputArray {
|
|
|
|
YYJSONInputArray(yyjson_val* _val) : val_(_val) {}
|
|
|
|
yyjson_val* val_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct YYJSONInputObject {
|
|
|
|
YYJSONInputObject(yyjson_val* _val) : val_(_val) {}
|
|
|
|
yyjson_val* val_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct YYJSONInputVar {
|
|
|
|
YYJSONInputVar() : val_(nullptr) {}
|
|
|
|
YYJSONInputVar(yyjson_val* _val) : val_(_val) {}
|
|
|
|
yyjson_val* val_;
|
|
|
|
};
|
|
|
|
|
|
|
|
using InputArrayType = YYJSONInputArray;
|
|
|
|
using InputObjectType = YYJSONInputObject;
|
|
|
|
using InputVarType = YYJSONInputVar;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static constexpr bool has_custom_constructor =
|
2024-06-16 00:13:15 -04:00
|
|
|
(requires(InputVarType var) { T::from_json_obj(var); });
|
2024-06-08 14:10:59 -04:00
|
|
|
|
2024-06-16 00:13:15 -04:00
|
|
|
rfl::Result<InputVarType> get_field(const std::string& _name, const InputObjectType _obj)
|
|
|
|
const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
const auto var = InputVarType(yyjson_obj_get(_obj.val_, _name.c_str()));
|
|
|
|
if (!var.val_) {
|
|
|
|
return rfl::Error("Object contains no field named '" + _name + "'.");
|
|
|
|
}
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_empty(const InputVarType _var) const noexcept {
|
|
|
|
return !_var.val_ || yyjson_is_null(_var.val_);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ArrayReader>
|
2024-06-16 00:13:15 -04:00
|
|
|
std::optional<Error> read_array(const ArrayReader& _array_reader, const InputArrayType& _arr)
|
|
|
|
const noexcept {
|
2024-06-08 15:53:06 -04:00
|
|
|
yyjson_val* val;
|
2024-06-08 14:10:59 -04:00
|
|
|
yyjson_arr_iter iter;
|
|
|
|
yyjson_arr_iter_init(_arr.val_, &iter);
|
|
|
|
while ((val = yyjson_arr_iter_next(&iter))) {
|
|
|
|
const auto err = _array_reader.read(InputVarType(val));
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
}
|
|
|
|
return std::nullopt;
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
template <class ObjectReader>
|
2024-06-16 00:13:15 -04:00
|
|
|
std::optional<Error>
|
|
|
|
read_object(const ObjectReader& _object_reader, const InputObjectType& _obj) const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
yyjson_obj_iter iter;
|
|
|
|
yyjson_obj_iter_init(_obj.val_, &iter);
|
|
|
|
yyjson_val* key;
|
|
|
|
while ((key = yyjson_obj_iter_next(&iter))) {
|
|
|
|
const auto name = std::string_view(yyjson_get_str(key));
|
|
|
|
_object_reader.read(name, InputVarType(yyjson_obj_iter_get_val(key)));
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
rfl::Result<T> to_basic_type(const InputVarType _var) const noexcept {
|
|
|
|
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
|
|
|
const auto r = yyjson_get_str(_var.val_);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (r == NULL) {
|
|
|
|
return rfl::Error("Could not cast to string.");
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return std::string(r);
|
|
|
|
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
|
|
|
if (!yyjson_is_bool(_var.val_)) {
|
|
|
|
return rfl::Error("Could not cast to boolean.");
|
|
|
|
}
|
|
|
|
return yyjson_get_bool(_var.val_);
|
|
|
|
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
|
|
|
if (!yyjson_is_num(_var.val_)) {
|
|
|
|
return rfl::Error("Could not cast to double.");
|
|
|
|
}
|
|
|
|
return static_cast<T>(yyjson_get_num(_var.val_));
|
|
|
|
} else if constexpr (std::is_unsigned<std::remove_cvref_t<T>>()) {
|
|
|
|
if (!yyjson_is_int(_var.val_)) {
|
|
|
|
return rfl::Error("Could not cast to int.");
|
|
|
|
}
|
|
|
|
return static_cast<T>(yyjson_get_uint(_var.val_));
|
|
|
|
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
|
|
|
if (!yyjson_is_int(_var.val_)) {
|
|
|
|
return rfl::Error("Could not cast to int.");
|
|
|
|
}
|
|
|
|
return static_cast<T>(yyjson_get_sint(_var.val_));
|
|
|
|
} else {
|
|
|
|
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
|
|
|
}
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
2024-06-16 00:13:15 -04:00
|
|
|
rfl::Result<InputArrayType> to_array(const InputVarType _var) const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
if (!yyjson_is_arr(_var.val_)) {
|
|
|
|
return rfl::Error("Could not cast to array!");
|
|
|
|
}
|
|
|
|
return InputArrayType(_var.val_);
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
2024-06-16 00:13:15 -04:00
|
|
|
rfl::Result<InputObjectType> to_object(const InputVarType _var) const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
if (!yyjson_is_obj(_var.val_)) {
|
|
|
|
return rfl::Error("Could not cast to object!");
|
|
|
|
}
|
|
|
|
return InputObjectType(_var.val_);
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
template <class T>
|
2024-06-16 00:13:15 -04:00
|
|
|
rfl::Result<T> use_custom_constructor(const InputVarType _var) const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
try {
|
|
|
|
return T::from_json_obj(_var);
|
|
|
|
} catch (std::exception& e) { return rfl::Error(e.what()); }
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace json
|
|
|
|
} // namespace rfl
|
|
|
|
|
|
|
|
#endif // JSON_PARSER_HPP_
|