Skip to content

Conversion to STL map<string, vector<int>> gives error #220

@pnth

Description

@pnth

I tried to convert a json object to and from STL map<string, vector<int>>. The "from" direction works well:

map<string, vector<int> > c_map{ { "one", { 1, 2 } }, { "two", { 3, 4 } }, { "three", { 5, 6 } } };
json j_map(c_map);
cout << j_map.dump() << endl;
// Output: {"one":[1,2],"three":[5,6],"two":[3,4]}

But the "to" direction gives the following error:

map<string, vector<int> > v2 = j_map;
or
map<string, vector<int> > v2 = j_map.get<map<string, vector<int>>>();

/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/5.3.0/../../../../include/c++/5
.3.0/bits/stl_pair.h:125:22: error: call to constructor of 'std::vector<int, s
td::allocator<int> >' is ambiguous

Is there a way to get around this error?

The library does conversions fine for map<string, int>

map<string, int> c_map{ { "one", 1 }, { "two", 4 }, { "three", 6 } };
json j_map(c_map);
cout << j_map.dump() << endl;
map<string, int > v2 = j_map;

I also tried this but did not work either

map<string, vector<int> > v2;
v2["one"] = j_map["one"];
main.cpp:249:13: error: use of overloaded operator '=' is ambiguous (with oper
and types 'mapped_type' (aka 'std::vector<int, std::allocator<int> >') and 'va
lue_type' (aka 'nlohmann::basic_json<std::map, std::vector, std::__cxx11::basi
c_string<char>, bool, long, unsigned long, double, std::allocator>'))

This final one works but looks ugly:

map<string, vector<int> > v2;
vector<int> one = j_map["one"];
v2["one"] = one;
vector<int> two = j_map["two"];
v2["two"] = two;

Many thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmedsolution: wontfixthe issue will not be fixed (either it is impossible or deemed out of scope)state: help neededthe issue needs help to proceed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions