Add emplace-unmarshaling (experimental)

This commit is contained in:
G.H.O.S.T 2024-12-31 00:54:03 +01:00
parent 5ab48d8c36
commit 729522038c
Signed by: G.H.O.S.T
GPG Key ID: 3BD93EABD1407B82

View File

@ -260,6 +260,46 @@ namespace code::json
};
template<member_name_t Name, typename Type>
struct emplace_member_t
{
using T = Type;
static constexpr bool is_optional{
is_optional_v<T>
};
static
T
unmarshal(variant const& v, marshaling_context_t* context)
{
if (!v.contains(Name.str())) {
if constexpr (is_optional) {
return std::nullopt;
}
throw std::runtime_error{"missing field '" + Name.str() + "'"};
}
return marshaling_traits<T>::unmarshal(v.get(Name), context);
}
};
template<typename T, typename... Members>
struct emplace_t
{
static
T
unmarshal(variant const& v, marshaling_context_t* context)
{
return T{
Members::unmarshal(v, context)...
};
}
};
template<typename T>
struct marshaling_traits
{