-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
add pair/tuple conversions #624
add pair/tuple conversions #624
Conversation
What would be the behavior if I try to convert |
It would throw, I use |
Right, and it makes sense to complain about this (I guess it's the same behavior when you try to read four values from a three-value tuple?). I think discarding values is OK, too, as long as we mention this in the documentation somewhere. |
Yes you get a compile-time error if you go out of bounds with |
In #553, we excluded a conversion from |
src/json.hpp
Outdated
template <typename BasicJsonType, typename... Args> | ||
void from_json(const BasicJsonType& j, std::pair<Args...>& p) | ||
{ | ||
// should we check that j.size() exactly matches the number of arguments for tuple and pairs? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's OK not to check this. The comment can be removed.
CHECK(j[3][0] == 0); | ||
CHECK(j[3][1] == 1); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add a test case for the case when we have an array with, say, 3 elements and ask for a std::pair
and something similar with tuples?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it is, sorry for the delay.
80f701e
to
6e4910d
Compare
I will take a look at Edit: It might be better to check the JSON array size, and throw a nice exception, instead of relying on Edit2: While we're at it, I think we should discuss about pre/postconditions of |
@theodelrieu Yes, a nice exception would definitely help. I guess most people would never expect And yes, the pre/post conditions should be documented. I think the README paragraph is the best place to start. |
Thanks a lot, @theodelrieu ! |
I left a question in the code: Should we impose that the JSON array size must be equal to the number of tuple/pair elements?
If not, some elements in the JSON could be discarded:
Some users might want to discard values on purpose, so I would say we should not check the size at all.