Skip to content

Commit

Permalink
Augment json_type_traits errors with member name
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Sep 22, 2023
1 parent a414677 commit 8efb52b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
15 changes: 15 additions & 0 deletions include/jsoncons/json_exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ namespace jsoncons {
#endif
};

template <class CharT>
std::string append_key_to_message(const std::string& message, const jsoncons::basic_string_view<CharT>& 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;
Expand Down
18 changes: 16 additions & 2 deletions include/jsoncons/json_traits_macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ namespace jsoncons
template <class OutputType>
static void set_udt_member(const Json& j, const string_view_type& key, OutputType& val)
{
val = j.at(key).template as<OutputType>();
try
{
val = j.at(key).template as<OutputType>();
}
catch (const std::exception& e)
{
JSONCONS_THROW(json_runtime_error<std::runtime_error>(append_key_to_message(e.what(), key)));
}
}

template <class T, class From, class OutputType>
Expand All @@ -68,7 +75,14 @@ namespace jsoncons
template <class T, class From, class OutputType>
static void set_udt_member(const Json& j, const string_view_type& key, From from, OutputType& val)
{
val = from(j.at(key).template as<T>());
try
{
val = from(j.at(key).template as<T>());
}
catch (const std::exception& e)
{
JSONCONS_THROW(json_runtime_error<std::runtime_error>(append_key_to_message(e.what(), key)));
}
}
template <class U>
static void set_optional_json_member(const string_view_type& key, const std::shared_ptr<U>& val, Json& j)
Expand Down

0 comments on commit 8efb52b

Please sign in to comment.