Skip to content

Cannot convert from json array to std::array #553

@wgtdkp

Description

@wgtdkp

Conversion from std::array to json array works well, but not in reverse.

std::string r = R"([1, 2, 3, 4])";
std::array<int, 4> arr = json::parse(r);

clang++ gives compiling errors:

/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_iterator.h:661:20: error: 
      no member named 'insert' in 'std::array<int, 4>'
        iter = container->insert(iter, std::move(__value));
               ~~~~~~~~~  ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:4175:12: note: 
      in instantiation of member function 'std::insert_iterator<std::array<int, 4> >::operator='
      requested here
        *__result = __unary_op(*__first);
                  ^
../../src/json.hpp:720:10: note: in instantiation of function template specialization
      'std::transform<nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char>, bool,
      long, unsigned long, double, std::allocator, adl_serializer>::iter_impl<const
      nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char>, bool, long, unsigned
      long, double, std::allocator, adl_serializer> >, std::insert_iterator<std::array<int, 4> >, (lambda
      at ../../src/json.hpp:721:50)>' requested here
    std::transform(j.begin(), j.end(),
         ^
../../src/json.hpp:804:5: note: in instantiation of function template specialization
      'nlohmann::detail::from_json_array_impl<nlohmann::basic_json<std::map, std::vector,
      std::__cxx11::basic_string<char>, bool, long, unsigned long, double, std::allocator,
      adl_serializer>, std::array<int, 4> >' requested here
    from_json_array_impl(j, arr, priority_tag<1> {});
    ^
../../src/json.hpp:902:16: note: in instantiation of function template specialization
      'nlohmann::detail::from_json<nlohmann::basic_json<std::map, std::vector,
      std::__cxx11::basic_string<char>, bool, long, unsigned long, double, std::allocator,
      adl_serializer>, std::array<int, 4>, 0>' requested here
        return from_json(j, val);
               ^
../../src/json.hpp:917:16: note: in instantiation of function template specialization
      'nlohmann::detail::from_json_fn::call<nlohmann::basic_json<std::map, std::vector,
      std::__cxx11::basic_string<char>, bool, long, unsigned long, double, std::allocator,
      adl_serializer>, std::array<int, 4> >' requested here
        return call(j, val, priority_tag<1> {});
               ^
../../src/json.hpp:964:9: note: in instantiation of function template specialization
      'nlohmann::detail::from_json_fn::operator()<nlohmann::basic_json<std::map, std::vector,
      std::__cxx11::basic_string<char>, bool, long, unsigned long, double, std::allocator,
      adl_serializer>, std::array<int, 4> >' requested here
        ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
        ^
../../src/json.hpp:3272:36: note: in instantiation of function template specialization
      'nlohmann::adl_serializer<std::array<int, 4>, void>::from_json<const nlohmann::basic_json<std::map,
      std::vector, std::__cxx11::basic_string<char>, bool, long, unsigned long, double, std::allocator,
      adl_serializer> &, std::array<int, 4> >' requested here
        JSONSerializer<ValueType>::from_json(*this, ret);
                                   ^
../../src/json.hpp:3530:16: note: in instantiation of function template specialization
      'nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char>, bool, long, unsigned
      long, double, std::allocator, adl_serializer>::get<std::array<int, 4>, std::array<int, 4>, 0>'
      requested here
        return get<ValueType>();
               ^
array.cc:8:28: note: in instantiation of function template specialization 'nlohmann::basic_json<std::map,
      std::vector, std::__cxx11::basic_string<char>, bool, long, unsigned long, double, std::allocator,
      adl_serializer>::operator array<std::array<int, 4>, 0>' requested here
  std::array<int, 4> arr = json::parse(r);
                           ^
1 error generated.

It seems we use std::inserter to transform json array to std::array, but std::array doesn't support insertion.
Is it designed so?
It seems unreasonable if we can convert std::array to json array but cannot do the same in reverse(and this could be easily supported, isn't it?).

My environment:

  1. clang++ = 3.8.0
  2. ubuntu = 16.04
  3. linux kernel = 4.4.0

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions