25#include "char8_t_remediation.hpp"
26#include "error_extensions.hpp"
27#include "exception.hpp"
28#include "json_compat.hpp"
29#include "meta/naming_convention.hpp"
30#include "reflection.hpp"
42 template <
typename Visitor>
43 decltype(
auto) visit_json(
const json& value, Visitor&& visitor) {
44 using nlohmann::detail::value_t;
46 switch (
value.type()) {
48 return std::forward<Visitor>(visitor)(
nullptr);
50 return std::forward<Visitor>(visitor)(
value);
53 case value_t::boolean:
54 return std::forward<Visitor>(visitor)(
value.get<
bool>());
56 return std::forward<Visitor>(visitor)(
value.get<std::string>());
57 case value_t::number_float:
58 return std::forward<Visitor>(visitor)(
value.get<
double>());
59 case value_t::number_integer:
60 return std::forward<Visitor>(visitor)(
value.get<std::int64_t>());
61 case value_t::number_unsigned:
62 return std::forward<Visitor>(visitor)(
value.get<std::uint64_t>());
64 return std::forward<Visitor>(visitor)(
value);
70 template <meta::reflectable T, meta::naming_convention Convention>
72 using fields_t = meta::get_fields_t<T>;
74 static void from_json(
const json& value, T& obj) {
75 for_each_as_values<fields_t>([&]<
typename... Ts>(std::type_identity<Ts>...) {
77 (value.value(meta::convert_naming_convention(Ts::name, Convention), json{}).get_to(Ts::value_ref(obj)),
82 static void to_json(json& value,
const T& obj) {
83 for_each_as_values<fields_t>([&]<
typename... Ts>(std::type_identity<Ts>...) {
84 auto handler = [&]<
typename U>(std::type_identity<U>) {
85 if (json intermediate(U::value_ref(obj)); !intermediate.is_null()) {
86 value.emplace(meta::convert_naming_convention(U::name, Convention), std::move(intermediate));
90 (handler(std::type_identity<Ts>{}), ...);
95 template <json_serializable T>
96 T deserialize_json(
const std::filesystem::path& path) {
97 return throw_nested_and_flatten(
99 std::ifstream stream{path, std::ios::binary};
101 stream.exceptions(std::ifstream::failbit);
103 return json::parse(stream).get<T>();
108#define ES_JSON_SERIALIZABLE(naming_convention) \
109 friend void from_json(const essence::json& value, es_meta_this_type& obj) { \
110 essence::json_serializer<es_meta_this_type, naming_convention>::from_json(value, obj); \
113 friend void to_json(essence::json& value, const es_meta_this_type& obj) { \
114 essence::json_serializer<es_meta_this_type, naming_convention>::to_json(value, obj); \
117 ES_MAKE_TERMINATOR(__COUNTER__)
119#define ES_JSON_SERIALIZABLE_DEFAULT ES_JSON_SERIALIZABLE(essence::meta::naming_convention::camel)
namespace for Niels Lohmann
Definition json.hpp:19415
ArrayType< basic_json, AllocatorType< basic_json > > array_t
a type for an array
Definition json.hpp:19657
@ value
the parser finished reading a JSON value
Definition json_extensions.hpp:71
An exception class derived from std::runtime_error that provides source code information of the sourc...
Definition error_extensions.hpp:73