Skip to content

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

Closed

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

Activity

  1. nlohmann commented on Mar 6, 2016

    @nlohmann
    Owner

    This could be related to #176.

  2. nlohmann commented on Mar 6, 2016

    @nlohmann
    Owner

    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]'
    
  3. nlohmann commented on Mar 6, 2016

    @nlohmann
    Owner
    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.

  4. nlohmann commented on May 9, 2016

    @nlohmann
    Owner

    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.

  5. added
    state: help needed
    on May 9, 2016
  6. added
    solution: wontfix
    on Jul 31, 2016
  7. nlohmann commented on Jul 31, 2016

    @nlohmann
    Owner

    It seems that no one has an idea about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmedsolution: wontfixstate: help needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions