Skip to content

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

Description

@rupertsteel

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmedsolution: wontfixthe issue will not be fixed (either it is impossible or deemed out of scope)state: help neededthe issue needs help to proceed

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions