diff --git a/include/jsoncons/json_exception.hpp b/include/jsoncons/json_exception.hpp index 323797b973..fd4ad174dc 100644 --- a/include/jsoncons/json_exception.hpp +++ b/include/jsoncons/json_exception.hpp @@ -228,6 +228,21 @@ namespace jsoncons { #endif }; +template +std::string append_key_to_message(const std::string& message, const jsoncons::basic_string_view& key) +{ + std::string s(message); + s.append(": "); + JSONCONS_TRY + { + unicode_traits::convert(key.data(), key.length(), s, unicode_traits::conv_flags::strict); + } + JSONCONS_CATCH(...) + { + } + return s; +} + #if !defined(JSONCONS_NO_DEPRECATED) JSONCONS_DEPRECATED_MSG("Instead, use ser_error") typedef ser_error serialization_error; JSONCONS_DEPRECATED_MSG("Instead, use ser_error") typedef ser_error json_parse_exception; diff --git a/include/jsoncons/json_traits_macros.hpp b/include/jsoncons/json_traits_macros.hpp index f545357779..7600d99b0e 100644 --- a/include/jsoncons/json_traits_macros.hpp +++ b/include/jsoncons/json_traits_macros.hpp @@ -58,7 +58,14 @@ namespace jsoncons template static void set_udt_member(const Json& j, const string_view_type& key, OutputType& val) { - val = j.at(key).template as(); + try + { + val = j.at(key).template as(); + } + catch (const std::exception& e) + { + JSONCONS_THROW(json_runtime_error(append_key_to_message(e.what(), key))); + } } template @@ -68,7 +75,14 @@ namespace jsoncons template static void set_udt_member(const Json& j, const string_view_type& key, From from, OutputType& val) { - val = from(j.at(key).template as()); + try + { + val = from(j.at(key).template as()); + } + 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::shared_ptr& val, Json& j)