-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Provide a from_json overload for std::map #1089
Conversation
@nlohmann Since we don't check extra values in the array when converting to (i.e. I'm wondering if we should check if the insertion in the map was successful, seems like a good idea. I don't know which exception to throw however. What's your opinion on it? |
6b6e73d
to
0b94826
Compare
0b94826
to
d44a273
Compare
Sorry, I haven't found the time to look into this earlier. |
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.
The rest of the PR is OK, just one little test case would be nice.
test/src/unit-conversions.cpp
Outdated
CHECK(m == m2); | ||
|
||
json j7 = {0, 1, 2, 3}; | ||
CHECK_THROWS_WITH((j7.get<std::map<int, int>>()), "[json.exception.type_error.302] type must be array, but is number"); |
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 please also add a CHECK_THROWS_AS
test?
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.
Done
I think the behavior is OK as you're right: it is consistent to the |
d44a273
to
30d4789
Compare
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.
Looks good to me.
Nothing is breaking, unless I missed something. The The only way that I know of constructing this peculiar Also, one can already serialize a |
With "breaking" I meant the API behavior in case of superfluous list entries. As we do not throw for pairs if we convert from a 3-element list, we should not throw for maps for symmetry reasons. But adding an exception to the pairs would be a breaking change. |
This overload is chosen only when BasicJsonType::string_t is not constructible from std::map::key_type. Currently, converting a map to json treats it as an array of pairs. fixes nlohmann#1079
30d4789
to
c5e63fd
Compare
Oh you're right, I've added a test case for this, I kept the existing behavior (i.e. discarding extra values from arrays) |
Just for my understanding: Assume I have Assume I have |
Indeed, it will be serialized to an object. The condition is: if |
Thanks a lot! |
This overload is chosen only when BasicJsonType::string_t
is not constructible from std::map::key_type.
Currently, converting a map to json treats it as an array of pairs.
fixes #1079
Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filesingle_include/nlohmann/json.hpp
. The whole process is described here.