You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Newbie issue: how does one convert a map in Json back to std::map? #600
Interestingly, the issue is if the value is a std::string.
This works:
#include"json.hpp"using json = nlohmann::json;
intmain()
{
// 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 succeedsassert(m1 == m2);
}
This does not:
#include"json.hpp"using json = nlohmann::json;
intmain()
{
// 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 succeedsassert(m1 == m2);
}
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
This is due to the implementation of from_json(CompatibleObjectType), where we call the iterator-range constructor of that type.
It's weird that it works with pair<string, int> though. I suspect an implementation detail of map or pair that uses a different code path for pair<string, string>.
I think we should first convert from all BasicJsonType underlying pairs to CompatibleObjectType underlying pairs explicitely, and then create the CompatibleObjectType from those pairs.
Activity
nlohmann commentedon Jun 1, 2017
I assume
jis of typenlohmann::json?KennethThompson commentedon Jun 1, 2017
Correct
nlohmann commentedon Jun 1, 2017
Interestingly, the issue is if the value is a
std::string.This works:
This does not:
nlohmann commentedon Jun 1, 2017
Error message:
KennethThompson commentedon Jun 1, 2017
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
KennethThompson commentedon Jun 1, 2017
I'm glad it wasn't me being a total newbie. It seemed like I was using valid syntax.
nlohmann commentedon Jun 1, 2017
Just for reference - how does your solution look like?
KennethThompson commentedon Jun 1, 2017
I'll post it once I get back to my home computer. Unfortunately, my day job does not include compilers anymore.
KennethThompson commentedon Jun 2, 2017
This was my solution:
KennethThompson commentedon Jun 2, 2017
note that if you take the above and do a std::string(element.value()), the same error occurs as you posted above
theodelrieu commentedon Jun 2, 2017
This is due to the implementation of
from_json(CompatibleObjectType), where we call the iterator-range constructor of that type.It's weird that it works with
pair<string, int>though. I suspect an implementation detail ofmaporpairthat uses a different code path forpair<string, string>.I think we should first convert from all
BasicJsonTypeunderlyingpairs toCompatibleObjectTypeunderlyingpairs explicitely, and then create theCompatibleObjectTypefrom those pairs.KennethThompson commentedon Jun 2, 2017
I think this should be tagged as a confirmed bug
nlohmann commentedon Jun 5, 2017
@theodelrieu Do you see a way to fix this?
theodelrieu commentedon Jun 6, 2017
I will try to fix it this morning, I'll take a look at #607 too
8 remaining items
nlohmann commentedon Jun 17, 2017
You are right, this still works. I shall add a regression test and close this issue.
✅ regression test for #600
theodelrieu commentedon Jun 17, 2017
Actually, the test is inside
unit-constructor1.cpp, I should have put it inunit-regression.cppnlohmann commentedon Jun 17, 2017
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.
amibae commentedon Jun 7, 2018
Is it possible that there is still an error when using the assignment operator of std::map?
For example:
theodelrieu commentedon Jun 7, 2018
Hello @amibae , the root of this problem is explained in detail in #958.
TheWaveWarden commentedon Nov 23, 2018
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?
theodelrieu commentedon Nov 23, 2018
@FrotBot What is the compiler error?
TheWaveWarden commentedon Nov 23, 2018
theodelrieu commentedon Nov 23, 2018
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.
TheWaveWarden commentedon Nov 23, 2018
yes, thank you very much!
#1372
ipodipad commentedon Mar 26, 2019
This works for me.
Is there any ever better solution on this?