-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support nlohmann::basic_json::value with JSON_NOEXCEPTION #1738
Comments
The code with template<class ValueType, typename std::enable_if<
std::is_convertible<basic_json_t, ValueType>::value, int>::type = 0>
ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
{
// at only works for objects
if (JSON_HEDLEY_LIKELY(is_object()))
{
// if key is found, return value and given default value otherwise
const auto it = find(key);
if (it != end())
{
return *it;
}
return default_value;
}
JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
} An exception is only thrown in case the value is not an object. Would that work for you? |
Sorry, I didn't specify that I was referring to the |
We could realize this via the |
This would solve my problem. I had thought of |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Currently, accessing an objects element through
value
(to get a default value if not present) does not work if compiled withJSON_NOEXCEPTION
. This is because it is implemented similar to:If exceptions are disabled, this terminates the program even though it is obvious what the user wanted, namely the provided default value. Why is this not implemented similar to this:
As far as I can see, this has the described semantics (except that the description currently says that it is based on exceptions) and the same complexity, but works with
JSON_NOEXCEPTION
.The text was updated successfully, but these errors were encountered: