Skip to content
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

Closed
ingomueller-net opened this issue Sep 5, 2019 · 5 comments
Closed

Support nlohmann::basic_json::value with JSON_NOEXCEPTION #1738

ingomueller-net opened this issue Sep 5, 2019 · 5 comments
Labels
kind: enhancement/improvement state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@ingomueller-net
Copy link

Currently, accessing an objects element through value (to get a default value if not present) does not work if compiled with JSON_NOEXCEPTION. This is because it is implemented similar to:

try {
    return at(key);
} catch(out_of_range) {
    return default_value;
}

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:

auto const it = find(key);
if (it != end()) return *it;
return default_value;

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.

@nlohmann
Copy link
Owner

nlohmann commented Sep 5, 2019

The code with at and try is only to describe the behavior. The actual implementation is this:

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?

@ingomueller-net
Copy link
Author

Sorry, I didn't specify that I was referring to the json_pointer overload.

@nlohmann
Copy link
Owner

nlohmann commented Sep 5, 2019

We could realize this via the contains function which only throws in case of invalid JSON pointers. I am not aware of a find overload for JSON pointers.

@ingomueller-net
Copy link
Author

This would solve my problem. I had thought of find to avoid two lookups (one for contains, one for at), but I haven't done much digging...

@stale
Copy link

stale bot commented Oct 5, 2019

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.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Oct 5, 2019
@stale stale bot closed this as completed Oct 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement/improvement state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

2 participants