diff --git a/include/jsoncons/json_traits_macros.hpp b/include/jsoncons/json_traits_macros.hpp index 7600d99b0e..55f9403f32 100644 --- a/include/jsoncons/json_traits_macros.hpp +++ b/include/jsoncons/json_traits_macros.hpp @@ -87,22 +87,59 @@ namespace jsoncons template static void set_optional_json_member(const string_view_type& key, const std::shared_ptr& val, Json& j) { - if (val) j.try_emplace(key, val); + if (val) + { + try + { + j.try_emplace(key, val); + } + catch (const std::exception& e) + { + JSONCONS_THROW(json_runtime_error(append_key_to_message(e.what(), key))); + } + } } template static void set_optional_json_member(const string_view_type& key, const std::unique_ptr& val, Json& j) { - if (val) j.try_emplace(key, val); + if (val) + { + try + { + j.try_emplace(key, val); + } + catch (const std::exception& e) + { + JSONCONS_THROW(json_runtime_error(append_key_to_message(e.what(), key))); + } + } } template static void set_optional_json_member(const string_view_type& key, const jsoncons::optional& val, Json& j) { - if (val) j.try_emplace(key, val); + if (val) + { + try + { + j.try_emplace(key, val); + } + catch (const std::exception& e) + { + JSONCONS_THROW(json_runtime_error(append_key_to_message(e.what(), key))); + } + } } template static void set_optional_json_member(const string_view_type& key, const U& val, Json& j) { - j.try_emplace(key, val); + try + { + j.try_emplace(key, val); + } + catch (const std::exception& e) + { + JSONCONS_THROW(json_runtime_error(append_key_to_message(e.what(), key))); + } } }; }