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

json::value(const json_pointer&, ValueType) requires exceptions to return the default value. #440

Closed
rupertsteel opened this issue Jan 31, 2017 · 6 comments
Labels
confirmed solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) state: help needed the issue needs help to proceed

Comments

@rupertsteel
Copy link

json::value(const json_pointer&, ValueType) is defined as:

template<class ValueType, typename std::enable_if<
             std::is_convertible<basic_json_t, ValueType>::value, int>::type = 0>
ValueType value(const json_pointer& ptr, ValueType default_value) const
{
    // at only works for objects
    if (is_object())
    {
        // if pointer resolves a value, return it or use default value
        JSON_TRY
        {
            return ptr.get_checked(this);
        }
        JSON_CATCH (std::out_of_range&)
        {
            return default_value;
        }
    }

    JSON_THROW(std::domain_error("cannot use value() with " + type_name()));
}

If exceptions are disabled, JSON_TRY turns into if (true) and JSON_CATCH turns into if (false), this means that the default value will never be returned if exceptions are turned off.

@nlohmann
Copy link
Owner

You are right - the function value currently relies on exceptions. I currently only see copying a lot of the implementation of get_checked() into the function to fix this.

@nlohmann nlohmann added confirmed state: help needed the issue needs help to proceed labels Feb 4, 2017
@nlohmann
Copy link
Owner

nlohmann commented Feb 5, 2017

I think the easiest would be to remove the function when exceptions are turned off.

@TurpentineDistillery
Copy link

I'm seeing that the code in certain places still uses throw instead of THROW - was that intentional or an oversight?

@nlohmann
Copy link
Owner

@TurpentineDistillery This is an oversight. Thanks for noting!!

@nlohmann
Copy link
Owner

Replaced the throw calls.

@nlohmann nlohmann added the solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) label Mar 28, 2017
@nlohmann
Copy link
Owner

Realizing this function without exceptions would mean doubling the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) state: help needed the issue needs help to proceed
Projects
None yet
Development

No branches or pull requests

3 participants