Skip to content

template-based as<type>() accessor #1066

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

Closed
JulianvDoorn opened this issue Oct 24, 2019 · 2 comments
Closed

template-based as<type>() accessor #1066

JulianvDoorn opened this issue Oct 24, 2019 · 2 comments

Comments

@JulianvDoorn
Copy link

JulianvDoorn commented Oct 24, 2019

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
It's slightly disappointed when I have to write a helper function which allows to determine the type of the lookup at compile time.
Describe the solution you'd like
A clear and concise description of what you want to happen.
I wish to be able to call JsonValue::as() as an alternative of JsonValue::asDouble().

Describe alternatives you've considered
I've written a function that uses metaprogramming features to determine the type which is being looked up at compile time. (assumes C++17, but I imagine an implementation is possible, SFINAE?):

template <class dat_type>
dat_type jsonValueAsType(const Json::Value& jsonVal) {
    if constexpr (std::is_same_v<dat_type, double>) {
        return jsonVal.asDouble();
    } else if constexpr (std::is_same_v<dat_type, float>) {
        return jsonVal.asFloat();
    }
}

Additional context
No additional context is required, I believe. But with focus of the C++ community to work towards better metaprogramming facilities, I think it is in place to add an JsonValue::as<T>() function.

@dota17
Copy link
Member

dota17 commented Oct 26, 2019

Please submit a PR if that's really what you want.

BillyDonahue added a commit to BillyDonahue/jsoncpp that referenced this issue Nov 3, 2019
Combine is<T> tests and combine as<T> tests.
@cdunn2001 cdunn2001 mentioned this issue Nov 4, 2019
@dota17
Copy link
Member

dota17 commented Nov 14, 2019

Closing as fixed.

@dota17 dota17 closed this as completed Nov 14, 2019
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

2 participants