Enable JSON marshaling inheritance

This commit is contained in:
G.H.O.S.T 2024-12-27 01:42:01 +01:00
parent 02ec014f35
commit eae4a7cfa8
Signed by: G.H.O.S.T
GPG Key ID: 3BD93EABD1407B82
2 changed files with 47 additions and 4 deletions

View File

@ -179,6 +179,49 @@ namespace code::json
return instance;
}
template<typename... Bases>
struct inherit_t
{
static_assert(sizeof...(Bases) > 0, "at least one base must be specified");
static
void
do_marshal(variant& v, T const& instance, marshaling_context_t* context)
{
((Bases::json::do_marshal(v, instance, context)), ...);
First::marshal(v, instance, context);
((Members::marshal(v, instance, context)), ...);
}
static
variant
marshal(T const& instance, marshaling_context_t* context)
{
variant v{std::map<std::string, variant>{}};
do_marshal(v, instance, context);
return v;
}
static
void
do_unmarshal(T& instance, variant const& v, marshaling_context_t* context)
{
((Bases::json::do_unmarshal(instance, v, context)), ...);
First::unmarshal(instance, v, context);
((Members::unmarshal(instance, v, context)), ...);
}
static
T
unmarshal(variant const& v, marshaling_context_t* context)
{
T instance;
do_unmarshal(instance, v, context);
return instance;
}
};
};
template<typename T>

View File

@ -1,11 +1,11 @@
#ifndef code__json__marshaling__traits_hxx_
#define code__json__marshaling__traits_hxx_
#ifndef code__json__traits_hxx_
#define code__json__traits_hxx_
#include <code/json/optional.hxx>
#include <type_traits>
namespace code::json::marshaling {
namespace code::json {
// is_optional<T>.
//
@ -67,6 +67,6 @@ struct function_traits<
: function_traits<decltype(&Callable::operator())> {
};
} // namespace code::json::marshaling
} // namespace code::json
#endif