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

Request: access with default value. #353

Closed
smoofproof opened this issue Nov 5, 2016 · 9 comments
Closed

Request: access with default value. #353

smoofproof opened this issue Nov 5, 2016 · 9 comments

Comments

@smoofproof
Copy link

smoofproof commented Nov 5, 2016

Hi everyone!

I would suggest to provide new member functions

    template <typename T>
    T operator ()(const typename object_t::key_type& key, const T &fallback) const
    {
        const auto it = find(key);
        if (it == end() || it.value().is_null())
        {
            return fallback;
        }
        else
        {
            return at(key);
        }
    }

    string_t operator ()(const typename object_t::key_type& key, const string_t &fallback) const 
    {
        return operator()<string_t>(key, fallback);
    }

    template <typename T>
    T operator ()(const typename object_t::key_type& key, const T &fallback)
    {
        const auto it = find(key);
        if (it == end() || it.value().is_null())
        {
            return (*this)[key] = fallback;
        }
        else
        {
            return (*this)[key];
        }
    }

    string_t operator ()(const typename object_t::key_type& key, const string_t &fallback)
    {
        return operator()<string_t>(key, fallback);
    }

I use those for convenient accesses to numbers, strings, vectors and maps in a json object with default values.

Further, i use

    reference operator()(const typename object_t::key_type& key)
    {
        return (*this)[key];
    }

    value_type operator()(const typename object_t::key_type& key) const
    {
        if (find(key) == end())
        {
            return value_type();
        }
        else
        {
            return (*this)[key];
        }
    }

for more consistent code look when using the functions above.

What do you think?

Greetings

@nlohmann
Copy link
Owner

nlohmann commented Nov 5, 2016

@nlohmann
Copy link
Owner

@SmoofBullet Did you have the time to look at the value function?

@nlohmann nlohmann added state: please discuss please discuss the issue or vote for your favorite option kind: improvement and removed kind: improvement labels Nov 13, 2016
@zumm
Copy link

zumm commented Nov 14, 2016

@nlohmann method "value" helps to avoid outing of range only. If requested field exists, but has different type then this method throws exception. Such behavior is undesirable.

@nlohmann
Copy link
Owner

There has been quite a discussion about this, see #278.

@zumm
Copy link

zumm commented Nov 14, 2016

So this functional will be implemented in release 3.0.0? Great (apropos when it happens?)!

@nlohmann
Copy link
Owner

#278 is planned for the 3.0.0. This release will most likely not be ready this year.

If you can live with the proposal in #278, I would like to close this ticket.

@zumm
Copy link

zumm commented Nov 15, 2016

Yes, i can. :) But could starter of this ticket?

@nlohmann
Copy link
Owner

Oh, sorry. @SmoofBullet What do you think?

@smoofproof
Copy link
Author

I think the solution will work for me, although im not sure whether the value function should be restricted to object types. If i provide a default value, i would not mind if the caller passed me the wrong type (something different than object).

@nlohmann nlohmann removed the state: please discuss please discuss the issue or vote for your favorite option label Nov 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants