-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
object field accessors #103
Comments
Hi Roman, thanks for checking back. I understand why such a function would be interesting. I have one question: What is the advantage of a non-member function As for implementation, I would try to avoid the What do you think? I would like to avoid Cheers, |
I find myself using the "get with default value if missing" quite often as well. It would be a great addition to the member functions. For booleans I use something like this: static bool getBoolValueWithDefault(const json& jsonC, const std::string& key, const bool& default_value) { |
I think this could be solved with #133. |
Great! Thank you for the good work! |
Hi Niels,
not sure if this is needed inside the class, however that pattern is widely in use here, and might be of use for other users as well.
In a real implementation, names would need to be changed, and SFINAE type-checking shall be made if T is generally convertible from json's underlying types. It would also be good to avoid the internal exception, if possible, and have a non-throwing path (for me an exception is almost ever an error condition, whilst that new accessors gently asks "may I have it as T if that key is here"?
Another variant for c++14 and beyond users would be:
Which would make things even better/more precise.
Cheers,
Roman
PS: with gcc5 (stolen from their homepage) we can detect std::optional like this:
The text was updated successfully, but these errors were encountered: