diff --git a/code/json/marshaling.hxx b/code/json/marshaling.hxx index 817c710..0e12bbc 100644 --- a/code/json/marshaling.hxx +++ b/code/json/marshaling.hxx @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -41,11 +42,17 @@ namespace code::json std::copy_n(str, N, name); } - operator std::string const() const + std::string const + str() const { return name; } + operator std::string const() const + { + return str(); + } + char name[N]; }; @@ -67,6 +74,10 @@ namespace code::json using T = member_traits::class_type; using M = member_traits::member_type; + static constexpr bool is_optional{ + is_optional_v + }; + static void marshal(variant& v, T const& instance, marshaling_context_t* context) @@ -78,11 +89,22 @@ namespace code::json void unmarshal(T& instance, variant const& v, marshaling_context_t* context) { + if (!v.contains(Name.str())) { + if (is_optional) { + return; + } + + throw std::runtime_error{"missing field '" + Name.str() + "'"}; + } + instance.*Member = marshaling_traits::unmarshal(v.get(Name), context); } struct pointer_t { + using T = member_traits::class_type; + using M = member_traits::member_type; + static pointer const& ptr() { @@ -104,6 +126,10 @@ namespace code::json auto ptr_v = ptr().read(v); if (!ptr_v) { + if (is_optional) { + return; + } + throw std::runtime_error{ "missing field '" + to_string(ptr()) + "'" }; }