-
-
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
Newbie issue: how does one convert a map in Json back to std::map? #600
Comments
I assume |
Correct |
Interestingly, the issue is if the value is a This works: #include "json.hpp"
using json = nlohmann::json;
int main()
{
// create a map
std::map<std::string, int> m1 {{"key", 1}};
// create and print a JSON from the map
json j = m1;
std::cout << j << std::endl;
// get the map out of JSON
std::map<std::string, int> m2 = j;
// make sure the roundtrip succeeds
assert(m1 == m2);
} This does not: #include "json.hpp"
using json = nlohmann::json;
int main()
{
// create a map
std::map<std::string, std::string> m1 {{"key", "val"}};
// create and print a JSON from the map
json j = m1;
std::cout << j << std::endl;
// get the map out of JSON
std::map<std::string, std::string> m2 = j;
// make sure the roundtrip succeeds
assert(m1 == m2);
} |
Error message:
|
One work around I found is using an iterator feature that was presented in another un-related issue. With an iterator I can get access to each key/value pair. Perhaps this is a good work around until the issue is fully resolved |
I'm glad it wasn't me being a total newbie. It seemed like I was using valid syntax. |
Just for reference - how does your solution look like? |
I'll post it once I get back to my home computer. Unfortunately, my day job does not include compilers anymore. |
This was my solution: const json& rh = j["rightHand"];
for (auto& element : json::iterator_wrapper(rh)) {
std::cout << element.key() << " maps to " << element.value() << std::endl;
} |
note that if you take the above and do a std::string(element.value()), the same error occurs as you posted above |
This is due to the implementation of It's weird that it works with I think we should first convert from all |
I think this should be tagged as a confirmed bug |
@theodelrieu Do you see a way to fix this? |
I will try to fix it this morning, I'll take a look at #607 too |
Instead of calling CompatibleObjectType iterator-range constructor, first convert json::value_type to CompatibleObjectType::value_type
Add pair support, fix CompatibleObject conversions (fixes #600)
I think this issue is still solved with #615, there is a test that converts to |
You are right, this still works. I shall add a regression test and close this issue. |
Actually, the test is inside |
No, it's fine to have the tests inside the proper functional suite. But a regression test gives me a better feeling on top of it. |
Is it possible that there is still an error when using the assignment operator of std::map?
|
Hi I think there is still an issue with certain key/value type combinations. I can't seem to get <int, double> to work.
EDIT: Or is this due to the fact that int doesn't convert implicitly to string? |
@FrotBot What is the compiler error? |
|
Looks like something is broken with SFINAE checks. Sigh... I'll fix it on Monday, could you just open a new issue please? Thanks for the report. |
yes, thank you very much! |
This works for me. |
I have a map..
But when I go to retrieve the map I get an 'call of overload is ambiguous' type error:
Could you perhaps point me to where I can find an example of how to do this properly? I've searched but can't seem to find anything directly on point
Thanks!
The text was updated successfully, but these errors were encountered: