feat(reflect-cpp): Switched from glaze -> reflect cpp
A bug was discovered in glaze which prevented valid toml output. We have switched to toml++ and reflect-cpp. The interface has remained the same so this should not break any code
This commit is contained in:
15
build-config/reflect-cpp/include/rfl/json/Parser.hpp
Normal file
15
build-config/reflect-cpp/include/rfl/json/Parser.hpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef RFL_JSON_PARSER_HPP_
|
||||
#define RFL_JSON_PARSER_HPP_
|
||||
|
||||
#include "../parsing/Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
#include "Writer.hpp"
|
||||
|
||||
namespace rfl::json {
|
||||
|
||||
template <class T, class ProcessorsType>
|
||||
using Parser = parsing::Parser<Reader, Writer, T, ProcessorsType>;
|
||||
|
||||
} // namespace rfl::json
|
||||
|
||||
#endif
|
||||
164
build-config/reflect-cpp/include/rfl/json/Reader.hpp
Normal file
164
build-config/reflect-cpp/include/rfl/json/Reader.hpp
Normal file
@@ -0,0 +1,164 @@
|
||||
#ifndef RFL_JSON_READER_HPP_
|
||||
#define RFL_JSON_READER_HPP_
|
||||
|
||||
#if __has_include(<yyjson.h>)
|
||||
#include <yyjson.h>
|
||||
#else
|
||||
#include "../thirdparty/yyjson.h"
|
||||
#endif
|
||||
|
||||
#include <exception>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
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 =
|
||||
(requires(InputVarType var) { T::from_json_obj(var); });
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType _arr) const noexcept {
|
||||
const auto var = InputVarType(yyjson_arr_get(_arr.val_, _idx));
|
||||
if (!var.val_) {
|
||||
return error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return var;
|
||||
}
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType _obj) const noexcept {
|
||||
const auto var = InputVarType(yyjson_obj_get(_obj.val_, _name.c_str()));
|
||||
if (!var.val_) {
|
||||
return 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>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr) const noexcept {
|
||||
yyjson_val* val;
|
||||
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));
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(const ObjectReader& _object_reader,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
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;
|
||||
}
|
||||
|
||||
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_);
|
||||
if (r == NULL) {
|
||||
return error("Could not cast to string.");
|
||||
}
|
||||
return std::string(r);
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!yyjson_is_bool(_var.val_)) {
|
||||
return 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 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 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 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.");
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType _var) const noexcept {
|
||||
if (!yyjson_is_arr(_var.val_)) {
|
||||
return error("Could not cast to array!");
|
||||
}
|
||||
return InputArrayType(_var.val_);
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType _var) const noexcept {
|
||||
if (!yyjson_is_obj(_var.val_)) {
|
||||
return error("Could not cast to object!");
|
||||
}
|
||||
return InputObjectType(_var.val_);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
const InputVarType _var) const noexcept {
|
||||
try {
|
||||
return T::from_json_obj(_var);
|
||||
} catch (std::exception& e) {
|
||||
return error(e.what());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace json
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
146
build-config/reflect-cpp/include/rfl/json/Writer.hpp
Normal file
146
build-config/reflect-cpp/include/rfl/json/Writer.hpp
Normal file
@@ -0,0 +1,146 @@
|
||||
#ifndef RFL_JSON_WRITER_HPP_
|
||||
#define RFL_JSON_WRITER_HPP_
|
||||
|
||||
#if __has_include(<yyjson.h>)
|
||||
#include <yyjson.h>
|
||||
#else
|
||||
#include "../thirdparty/yyjson.h"
|
||||
#endif
|
||||
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
// #include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../common.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace json {
|
||||
|
||||
class RFL_API Writer {
|
||||
public:
|
||||
struct YYJSONOutputArray {
|
||||
YYJSONOutputArray(yyjson_mut_val* _val) : val_(_val) {}
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
struct YYJSONOutputObject {
|
||||
YYJSONOutputObject(yyjson_mut_val* _val) : val_(_val) {}
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
struct YYJSONOutputVar {
|
||||
YYJSONOutputVar(yyjson_mut_val* _val) : val_(_val) {}
|
||||
|
||||
YYJSONOutputVar(YYJSONOutputArray _arr) : val_(_arr.val_) {}
|
||||
|
||||
YYJSONOutputVar(YYJSONOutputObject _obj) : val_(_obj.val_) {}
|
||||
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
using OutputArrayType = YYJSONOutputArray;
|
||||
using OutputObjectType = YYJSONOutputObject;
|
||||
using OutputVarType = YYJSONOutputVar;
|
||||
|
||||
Writer();
|
||||
|
||||
~Writer() = default;
|
||||
|
||||
yyjson_mut_doc* doc() const { return doc_.get(); }
|
||||
|
||||
OutputArrayType array_as_root(const size_t) const noexcept;
|
||||
|
||||
OutputObjectType object_as_root(const size_t) const noexcept;
|
||||
|
||||
OutputVarType null_as_root() const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
const auto val = from_basic_type(_var);
|
||||
yyjson_mut_doc_set_root(doc(), val.val_);
|
||||
return OutputVarType(val);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t,
|
||||
OutputArrayType* _parent) const;
|
||||
|
||||
OutputArrayType add_array_to_object(const std::string_view& _name,
|
||||
const size_t,
|
||||
OutputObjectType* _parent) const;
|
||||
|
||||
OutputObjectType add_object_to_array(const size_t,
|
||||
OutputArrayType* _parent) const;
|
||||
|
||||
OutputObjectType add_object_to_object(const std::string_view& _name,
|
||||
const size_t,
|
||||
OutputObjectType* _parent) const;
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const {
|
||||
const auto val = from_basic_type(_var);
|
||||
const bool ok = yyjson_mut_arr_add_val(_parent->val_, val.val_);
|
||||
if (!ok) {
|
||||
throw std::runtime_error("Could not add value to array.");
|
||||
}
|
||||
return OutputVarType(val);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const {
|
||||
const auto val = from_basic_type(_var);
|
||||
const bool ok = yyjson_mut_obj_add(
|
||||
_parent->val_, yyjson_mut_strcpy(doc(), _name.data()), val.val_);
|
||||
if (!ok) {
|
||||
throw std::runtime_error("Could not add field '" + std::string(_name) +
|
||||
"' to object.");
|
||||
}
|
||||
return OutputVarType(val);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const;
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const;
|
||||
|
||||
void end_array(OutputArrayType*) const noexcept;
|
||||
|
||||
void end_object(OutputObjectType*) const noexcept;
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
return OutputVarType(yyjson_mut_strcpy(doc(), _var.c_str()));
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
return OutputVarType(yyjson_mut_bool(doc(), _var));
|
||||
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
return OutputVarType(yyjson_mut_real(doc(), static_cast<double>(_var)));
|
||||
|
||||
} else if constexpr (std::is_unsigned<std::remove_cvref_t<T>>()) {
|
||||
return OutputVarType(yyjson_mut_uint(doc(), static_cast<uint64_t>(_var)));
|
||||
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
return OutputVarType(yyjson_mut_int(doc(), static_cast<int64_t>(_var)));
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<yyjson_mut_doc> doc_;
|
||||
};
|
||||
|
||||
} // namespace json
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
22
build-config/reflect-cpp/include/rfl/json/load.hpp
Normal file
22
build-config/reflect-cpp/include/rfl/json/load.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef RFL_JSON_LOAD_HPP_
|
||||
#define RFL_JSON_LOAD_HPP_
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../io/load_string.hpp"
|
||||
#include "read.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace json {
|
||||
|
||||
template <class T, class... Ps>
|
||||
Result<T> load(const std::string& _fname, const yyjson_read_flag _flag = 0) {
|
||||
const auto read_string = [_flag](const auto& _str) {
|
||||
return read<T, Ps...>(_str, _flag);
|
||||
};
|
||||
return rfl::io::load_string(_fname).and_then(read_string);
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
64
build-config/reflect-cpp/include/rfl/json/read.hpp
Normal file
64
build-config/reflect-cpp/include/rfl/json/read.hpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#ifndef RFL_JSON_READ_HPP_
|
||||
#define RFL_JSON_READ_HPP_
|
||||
|
||||
#if __has_include(<yyjson.h>)
|
||||
#include <yyjson.h>
|
||||
#else
|
||||
#include "../thirdparty/yyjson.h"
|
||||
#endif
|
||||
|
||||
#include <istream>
|
||||
#include <string_view>
|
||||
|
||||
#include "../Processors.hpp"
|
||||
#include "../internal/wrap_in_rfl_array_t.hpp"
|
||||
#include "Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace json {
|
||||
|
||||
using InputObjectType = typename Reader::InputObjectType;
|
||||
using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from a JSON var.
|
||||
template <class T, class... Ps>
|
||||
auto read(const InputVarType& _obj) {
|
||||
const auto r = Reader();
|
||||
return Parser<T, Processors<Ps...>>::read(r, _obj);
|
||||
}
|
||||
|
||||
/// Parses an object from JSON using reflection.
|
||||
template <class T, class... Ps>
|
||||
Result<internal::wrap_in_rfl_array_t<T>> read(
|
||||
const std::string_view _json_str, const yyjson_read_flag _flag = 0) {
|
||||
if (_flag & YYJSON_READ_INSITU) {
|
||||
return error("YYJSON_READ_INSITU is not supported");
|
||||
}
|
||||
yyjson_read_err err;
|
||||
// According to yyjson's doc, it's safe castaway constness as long as
|
||||
// YYJSON_READ_INSITU is not set
|
||||
yyjson_doc* doc = yyjson_read_opts(const_cast<char*>(_json_str.data()),
|
||||
_json_str.size(), _flag, NULL, &err);
|
||||
if (!doc) {
|
||||
return error("Could not parse document: " + std::string(err.msg));
|
||||
}
|
||||
yyjson_val* root = yyjson_doc_get_root(doc);
|
||||
const auto r = Reader();
|
||||
auto res = Parser<T, Processors<Ps...>>::read(r, InputVarType(root));
|
||||
yyjson_doc_free(doc);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Parses an object from a stringstream.
|
||||
template <class T, class... Ps>
|
||||
auto read(std::istream& _stream, const yyjson_read_flag _flag = 0) {
|
||||
const auto json_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
return read<T, Ps...>(json_str, _flag);
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
31
build-config/reflect-cpp/include/rfl/json/save.hpp
Normal file
31
build-config/reflect-cpp/include/rfl/json/save.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef RFL_JSON_SAVE_HPP_
|
||||
#define RFL_JSON_SAVE_HPP_
|
||||
|
||||
#if __has_include(<yyjson.h>)
|
||||
#include <yyjson.h>
|
||||
#else
|
||||
#include "../thirdparty/yyjson.h"
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../io/save_string.hpp"
|
||||
#include "write.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace json {
|
||||
|
||||
template <class... Ps>
|
||||
Result<Nothing> save(const std::string& _fname, const auto& _obj,
|
||||
const yyjson_write_flag _flag = 0) {
|
||||
const auto write_func = [_flag](const auto& _obj_ref, auto& _stream) -> auto& {
|
||||
return write<Ps...>(_obj_ref, _stream, _flag);
|
||||
};
|
||||
return rfl::io::save_string(_fname, _obj, write_func);
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef RFL_JSON_SCHEMA_JSONSCHEMA_HPP_
|
||||
#define RFL_JSON_SCHEMA_JSONSCHEMA_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "../../Flatten.hpp"
|
||||
#include "../../Literal.hpp"
|
||||
#include "../../Rename.hpp"
|
||||
#include "Type.hpp"
|
||||
|
||||
namespace rfl::json::schema {
|
||||
|
||||
template <class T>
|
||||
struct JSONSchema {
|
||||
Rename<"$schema", Literal<"https://json-schema.org/draft/2020-12/schema">>
|
||||
schema{};
|
||||
rfl::Rename<"$comment", std::optional<std::string>> comment{};
|
||||
Flatten<T> root{};
|
||||
rfl::Rename<"$defs", std::map<std::string, Type>> definitions{};
|
||||
};
|
||||
|
||||
} // namespace rfl::json::schema
|
||||
|
||||
#endif
|
||||
153
build-config/reflect-cpp/include/rfl/json/schema/Type.hpp
Normal file
153
build-config/reflect-cpp/include/rfl/json/schema/Type.hpp
Normal file
@@ -0,0 +1,153 @@
|
||||
#ifndef RFL_JSON_SCHEMA_TYPE_HPP_
|
||||
#define RFL_JSON_SCHEMA_TYPE_HPP_
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "../../Literal.hpp"
|
||||
#include "../../Object.hpp"
|
||||
#include "../../Ref.hpp"
|
||||
#include "../../Rename.hpp"
|
||||
#include "../../Variant.hpp"
|
||||
|
||||
namespace rfl::json::schema {
|
||||
|
||||
/// The JSON representation of internal::schema::Type.
|
||||
struct Type {
|
||||
struct Boolean {
|
||||
std::optional<std::string> description{};
|
||||
Literal<"boolean"> type{};
|
||||
};
|
||||
|
||||
struct Integer {
|
||||
Literal<"integer"> type{};
|
||||
std::optional<std::string> description{};
|
||||
};
|
||||
|
||||
struct Number {
|
||||
Literal<"number"> type{};
|
||||
std::optional<std::string> description{};
|
||||
};
|
||||
|
||||
struct String {
|
||||
Literal<"string"> type{};
|
||||
std::optional<std::string> description{};
|
||||
rfl::Rename<"minLength", std::optional<size_t>> minSize{};
|
||||
rfl::Rename<"maxLength", std::optional<size_t>> maxSize{};
|
||||
};
|
||||
|
||||
using NumericType = rfl::Variant<Integer, Number>;
|
||||
|
||||
struct AllOf {
|
||||
std::optional<std::string> description{};
|
||||
std::vector<Type> allOf{};
|
||||
};
|
||||
|
||||
struct AnyOf {
|
||||
std::optional<std::string> description{};
|
||||
std::vector<Type> anyOf{};
|
||||
};
|
||||
|
||||
struct ExclusiveMaximum {
|
||||
std::optional<std::string> description{};
|
||||
rfl::Variant<double, int> exclusiveMaximum{};
|
||||
std::string type{};
|
||||
};
|
||||
|
||||
struct ExclusiveMinimum {
|
||||
std::optional<std::string> description{};
|
||||
rfl::Variant<double, int> exclusiveMinimum{};
|
||||
std::string type{};
|
||||
};
|
||||
|
||||
struct FixedSizeTypedArray {
|
||||
Literal<"array"> type{};
|
||||
std::optional<std::string> description{};
|
||||
rfl::Ref<Type> items{};
|
||||
size_t minItems{};
|
||||
size_t maxItems{};
|
||||
};
|
||||
|
||||
struct Maximum {
|
||||
std::optional<std::string> description{};
|
||||
rfl::Variant<double, int> maximum{};
|
||||
std::string type{};
|
||||
};
|
||||
|
||||
struct Minimum {
|
||||
std::optional<std::string> description{};
|
||||
rfl::Variant<double, int> minimum{};
|
||||
std::string type{};
|
||||
};
|
||||
|
||||
struct Null {
|
||||
Literal<"null"> type{};
|
||||
std::optional<std::string> description{};
|
||||
};
|
||||
|
||||
struct Object {
|
||||
Literal<"object"> type{};
|
||||
std::optional<std::string> description{};
|
||||
rfl::Object<Type> properties{};
|
||||
std::vector<std::string> required{};
|
||||
std::shared_ptr<Type> additionalProperties{};
|
||||
};
|
||||
|
||||
struct OneOf {
|
||||
std::optional<std::string> description{};
|
||||
std::vector<Type> oneOf{};
|
||||
};
|
||||
|
||||
struct Reference {
|
||||
Rename<"$ref", std::optional<std::string>> ref{};
|
||||
std::optional<std::string> description{};
|
||||
};
|
||||
|
||||
struct Regex {
|
||||
Literal<"string"> type{};
|
||||
std::optional<std::string> description{};
|
||||
std::string pattern{};
|
||||
};
|
||||
|
||||
struct StringEnum {
|
||||
Literal<"string"> type{};
|
||||
std::optional<std::string> description{};
|
||||
rfl::Rename<"enum", std::vector<std::string>> values{};
|
||||
};
|
||||
|
||||
struct StringMap {
|
||||
Literal<"object"> type{};
|
||||
std::optional<std::string> description{};
|
||||
rfl::Ref<Type> additionalProperties{};
|
||||
};
|
||||
|
||||
struct Tuple {
|
||||
Literal<"array"> type{};
|
||||
std::optional<std::string> description{};
|
||||
std::vector<Type> prefixItems{};
|
||||
bool items = false;
|
||||
};
|
||||
|
||||
struct TypedArray {
|
||||
Literal<"array"> type{};
|
||||
std::optional<std::string> description{};
|
||||
rfl::Ref<Type> items{};
|
||||
rfl::Rename<"minItems", std::optional<size_t>> minSize{};
|
||||
rfl::Rename<"maxItems", std::optional<size_t>> maxSize{};
|
||||
};
|
||||
|
||||
using ReflectionType =
|
||||
rfl::Variant<AllOf, AnyOf, Boolean, ExclusiveMaximum, ExclusiveMinimum,
|
||||
FixedSizeTypedArray, Integer, Maximum, Minimum, Number, Null,
|
||||
Object, OneOf, Reference, Regex, String, StringEnum,
|
||||
StringMap, Tuple, TypedArray>;
|
||||
|
||||
const auto& reflection() const { return value; }
|
||||
|
||||
ReflectionType value;
|
||||
};
|
||||
|
||||
} // namespace rfl::json::schema
|
||||
|
||||
#endif
|
||||
50
build-config/reflect-cpp/include/rfl/json/to_schema.hpp
Normal file
50
build-config/reflect-cpp/include/rfl/json/to_schema.hpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef RFL_JSON_TOSCHEMA_HPP_
|
||||
#define RFL_JSON_TOSCHEMA_HPP_
|
||||
|
||||
#if __has_include(<yyjson.h>)
|
||||
#include <yyjson.h>
|
||||
#else
|
||||
#include "../thirdparty/yyjson.h"
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
|
||||
// #include "../Literal.hpp"
|
||||
#include "../Processors.hpp"
|
||||
#include "../Variant.hpp"
|
||||
// #include "../parsing/schema/Type.hpp"
|
||||
// #include "../parsing/schema/ValidationType.hpp"
|
||||
#include "../parsing/schema/make.hpp"
|
||||
#include "Reader.hpp"
|
||||
#include "Writer.hpp"
|
||||
#include "schema/JSONSchema.hpp"
|
||||
// #include "schema/Type.hpp"
|
||||
// #include "write.hpp"
|
||||
#include "../common.hpp"
|
||||
|
||||
namespace rfl::json {
|
||||
|
||||
template <class T>
|
||||
struct TypeHelper {};
|
||||
|
||||
template <class... Ts>
|
||||
struct TypeHelper<rfl::Variant<Ts...>> {
|
||||
using JSONSchemaType = rfl::Variant<schema::JSONSchema<Ts>...>;
|
||||
};
|
||||
|
||||
RFL_API std::string to_schema_internal_schema(
|
||||
const parsing::schema::Definition& internal_schema, const yyjson_write_flag,
|
||||
const bool _no_required, const std::string& comment = "");
|
||||
|
||||
/// Returns the JSON schema for a class.
|
||||
template <class T, class... Ps>
|
||||
std::string to_schema(const yyjson_write_flag _flag = 0,
|
||||
const std::string& comment = "") {
|
||||
using P = Processors<Ps...>;
|
||||
const auto internal_schema = parsing::schema::make<Reader, Writer, T, P>();
|
||||
return to_schema_internal_schema(internal_schema, _flag,
|
||||
P::default_if_missing_, comment);
|
||||
}
|
||||
} // namespace rfl::json
|
||||
|
||||
#endif
|
||||
67
build-config/reflect-cpp/include/rfl/json/write.hpp
Normal file
67
build-config/reflect-cpp/include/rfl/json/write.hpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#ifndef RFL_JSON_WRITE_HPP_
|
||||
#define RFL_JSON_WRITE_HPP_
|
||||
|
||||
#include <stdexcept>
|
||||
#if __has_include(<yyjson.h>)
|
||||
#include <yyjson.h>
|
||||
#else
|
||||
#include "../thirdparty/yyjson.h"
|
||||
#endif
|
||||
|
||||
#include <ostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include "../Processors.hpp"
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace json {
|
||||
|
||||
/// Convenient alias for the YYJSON pretty flag
|
||||
inline constexpr yyjson_write_flag pretty = YYJSON_WRITE_PRETTY;
|
||||
|
||||
/// Returns a JSON string.
|
||||
template <class... Ps>
|
||||
std::string write(const auto& _obj, const yyjson_write_flag _flag = 0) {
|
||||
using T = std::remove_cvref_t<decltype(_obj)>;
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
auto w = Writer();
|
||||
Parser<T, Processors<Ps...>>::write(w, _obj, typename ParentType::Root{});
|
||||
yyjson_write_err err;
|
||||
const char* json_c_str =
|
||||
yyjson_mut_write_opts(w.doc(), _flag, NULL, NULL, &err);
|
||||
if (!json_c_str) {
|
||||
throw std::runtime_error("An error occured while writing to JSON: " +
|
||||
std::string(err.msg));
|
||||
}
|
||||
const auto json_str = std::string(json_c_str);
|
||||
free((void*)json_c_str);
|
||||
return json_str;
|
||||
}
|
||||
|
||||
/// Writes a JSON into an ostream.
|
||||
template <class... Ps>
|
||||
std::ostream& write(const auto& _obj, std::ostream& _stream,
|
||||
const yyjson_write_flag _flag = 0) {
|
||||
using T = std::remove_cvref_t<decltype(_obj)>;
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
auto w = Writer();
|
||||
Parser<T, Processors<Ps...>>::write(w, _obj, typename ParentType::Root{});
|
||||
yyjson_write_err err;
|
||||
const char* json_c_str =
|
||||
yyjson_mut_write_opts(w.doc(), _flag, NULL, NULL, &err);
|
||||
if (!json_c_str) {
|
||||
throw std::runtime_error("An error occured while writing to JSON: " +
|
||||
std::string(err.msg));
|
||||
}
|
||||
_stream << json_c_str;
|
||||
free((void*)json_c_str);
|
||||
return _stream;
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user