Skip to content
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

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

Closed
pnth opened this issue Mar 3, 2016 · 5 comments
Closed

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

pnth opened this issue Mar 3, 2016 · 5 comments
Labels
confirmed solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) state: help needed the issue needs help to proceed

Comments

@pnth
Copy link

pnth commented Mar 3, 2016

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

@nlohmann
Copy link
Owner

nlohmann commented Mar 6, 2016

This could be related to #176.

@nlohmann
Copy link
Owner

nlohmann commented Mar 6, 2016

I can reproduce the error.

Clang:

./../src/json.hpp:2450:20: note: in instantiation of function template specialization 'std::__1::map<std::__1::basic_string<char>, std::__1::vector<int, std::__1::allocator<int> >, std::__1::less<std::__1::basic_string<char> >, std::__1::allocator<std::__1::pair<const std::__1::basic_string<char>, std::__1::vector<int, std::__1::allocator<int> > > > >::map<std::__1::__map_iterator<std::__1::__tree_iterator<std::__1::__value_type<std::__1::basic_string<char>, nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long, unsigned long long, double, std::allocator> >, std::__1::__tree_node<std::__1::__value_type<std::__1::basic_string<char>, nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long, unsigned long long, double, std::allocator> >, void *> *, long> > >' requested here
            return T(m_value.object->begin(), m_value.object->end());

GCC:

../src/json.hpp:2450:68:   required from 'T nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType>::get_impl(T*) const [with T = std::map<std::__cxx11::basic_string<char>, std::vector<int> >; typename std::enable_if<(std::is_convertible<typename ObjectType<StringType, nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType>, std::less<StringType>, AllocatorType<std::pair<const StringType, nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType> > > >::key_type, typename T::key_type>::value && std::is_convertible<nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType>, typename T::mapped_type>::value), int>::type <anonymous> = 0; ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string<char>; BooleanType = bool; NumberIntegerType = long long int; NumberUnsignedType = long long unsigned int; NumberFloatType = double; AllocatorType = std::allocator]'

@nlohmann
Copy link
Owner

nlohmann commented Mar 6, 2016

json j = {1,2,3,4};
std::vector<int> v(j);

yields

issue220.cpp:15:22: error: call to constructor of 'std::vector<int>' is ambiguous
    std::vector<int> v(j);
                     ^ ~
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:489:40: note: candidate constructor
    _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
                                       ^
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:501:14: note: candidate constructor
    explicit vector(size_type __n);
             ^
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:537:5: note: candidate constructor
    vector(initializer_list<value_type> __il);
    ^
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:555:5: note: candidate constructor
    vector(vector&& __x)
    ^
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:549:5: note: candidate constructor
    vector(const vector& __x);
    ^
1 error generated.

This error message is similar to the one of

std::map<std::string, std::vector <int> > v2 = j_map;

which is

issue220.cpp:11:52: note: in instantiation of function template specialization 'nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long, unsigned long long, double, std::allocator>::operator map<std::__1::map<std::__1::basic_string<char>, std::__1::vector<int, std::__1::allocator<int> >, std::__1::less<std::__1::basic_string<char> >, std::__1::allocator<std::__1::pair<const std::__1::basic_string<char>, std::__1::vector<int, std::__1::allocator<int> > > > >, 0>' requested here
    std::map<std::string, std::vector <int> > v2 = j_map;
                                                   ^
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:489:40: note: candidate constructor
    _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
                                       ^
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:501:14: note: candidate constructor
    explicit vector(size_type __n);
             ^
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:537:5: note: candidate constructor
    vector(initializer_list<value_type> __il);
    ^
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:555:5: note: candidate constructor
    vector(vector&& __x)
    ^
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:549:5: note: candidate constructor
    vector(const vector& __x);
    ^
1 error generated.

Note that

json j = {1,2,3,4};
std::vector<int> v(j);

fails, whereas

json j = {1,2,3,4};
std::vector<int> v = j;

works.

@nlohmann
Copy link
Owner

nlohmann commented May 9, 2016

If I understand the problem correctly, std::vector<int> v(j); fails, because a json value can be implicitly converted to a lot of types, including size_type. I am not sure whether we can do anything about this.

@nlohmann nlohmann added the state: help needed the issue needs help to proceed label May 9, 2016
@nlohmann nlohmann added the solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) label Jul 31, 2016
@nlohmann
Copy link
Owner

It seems that no one has an idea about this.

@nlohmann nlohmann closed this as completed Nov 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) state: help needed the issue needs help to proceed
Projects
None yet
Development

No branches or pull requests

2 participants