Add emplace-unmarshaling (experimental)
This commit is contained in:
parent
5ab48d8c36
commit
729522038c
@ -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
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user