Skip to content

Deserializing std::array doesn't compiler (no insert()) #1071

Closed

Description

@Yitzi2

I've found that, at least in Visual Studio (but I suspect everywhere), attempting to convert a JSON into a std::array fails to compile, since std::array has a value type (and therefore uses the container deserializer), but no insert() method (which is used by the container deserializer). While not all that hard to work around, it'd be nice to have it fixed.

Activity

  1. nlohmann commented on May 1, 2018

    @nlohmann
    Owner

    I cannot reproduce the issue. This works with Xcode Version 9.3 (9E145):

    #include <iostream>
    #include "json.hpp"
    
    using json = nlohmann::json;
    
    int main() {
        json j = {23, 42, 99, 101, 113};
        std::array<int, 5> a = j;
    
        for (size_t i = 0; i < a.size(); ++i)
        {
            std::cout << "a[" << i << "] = " << a[i] << std::endl;
        }
    }

    What did you try? What was the error message?

  2. Yitzi2 commented on May 1, 2018

    @Yitzi2
    Author

    In my from_json for a class, I had the line:

        auto endpoints = j["endpoints"].get<std::array<glm::vec2, 2>>();

    (We already had defined a from_json for glm::vec2, and it was included above.)

    The error message (with our own files replaced by descriptions, and ellipses for some of our directory structure, since I'm not sure exactly what is and is not sensitive in that regard, and for some reason some angled brackets aren't showing up):

    1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.13.26128\include\iterator(107): error C2039: 'insert': is not a member of 'std::array<glm::vec2,2>'
    1>{header defining a class that uses the array}(38): note: see declaration of 'std::array<glm::vec2,2>'
    1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.13.26128\include\iterator(106): note: while compiling class template member function 'std::insert_iterator<CompatibleArrayType> &std::insert_iterator<CompatibleArrayType>::operator =(glm::tvec2<float,0> &&)'
    1>        with
    1>        [
    1>            CompatibleArrayType=std::array<glm::vec2,2>
    1>        ]
    1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.13.26128\include\algorithm(1328): note: see reference to function template instantiation 'std::insert_iterator<CompatibleArrayType> &std::insert_iterator<CompatibleArrayType>::operator =(glm::tvec2<float,0> &&)' being compiled
    1>        with
    1>        [
    1>            CompatibleArrayType=std::array<glm::vec2,2>
    1>        ]
    1>c:\...\thirdparty\json\nlohmann_json.hpp(721): note: see reference to class template instantiation 'std::insert_iterator<CompatibleArrayType>' being compiled
    1>        with
    1>        [
    1>            CompatibleArrayType=std::array<glm::vec2,2>
    1>        ]
    1>c:\...\thirdparty\json\nlohmann_json.hpp(766): note: see reference to function template instantiation 'void nlohmann::detail::from_json_array_impl<BasicJsonType,CompatibleArrayType>(const BasicJsonType &,CompatibleArrayType &,nlohmann::detail::priority_tag<0>)' being compiled
    1>        with
    1>        [
    1>            BasicJsonType=nlohmann::json,
    1>            CompatibleArrayType=std::array<glm::vec2,2>
    1>        ]
    1>c:\...\thirdparty\json\nlohmann_json.hpp(864): note: see reference to function template instantiation 'void nlohmann::detail::from_json<BasicJsonType,T,0>(const BasicJsonType &,CompatibleArrayType &)' being compiled
    1>        with
    1>        [
    1>            BasicJsonType=nlohmann::json,
    1>            T=std::array<glm::vec2,2>,
    1>            CompatibleArrayType=std::array<glm::vec2,2>
    1>        ]
    1>c:\...\thirdparty\json\nlohmann_json.hpp(879): note: see reference to function template instantiation 'void nlohmann::detail::from_json_fn::call<BasicJsonType,T>(const BasicJsonType &,T &,nlohmann::detail::priority_tag<1>) noexcept(false) const' being compiled
    1>        with
    1>        [
    1>            BasicJsonType=nlohmann::json,
    1>            T=std::array<glm::vec2,2>
    1>        ]
    1>c:\...\thirdparty\json\nlohmann_json.hpp(926): note: see reference to function template instantiation 'void nlohmann::detail::from_json_fn::operator ()<_Ty,ValueType>(const BasicJsonType &,T &) noexcept(false) const' being compiled
    1>        with
    1>        [
    1>            _Ty=nlohmann::json,
    1>            ValueType=std::array<glm::vec2,2>,
    1>            BasicJsonType=nlohmann::json,
    1>            T=std::array<glm::vec2,2>
    1>        ]
    1>c:\...\thirdparty\json\nlohmann_json.hpp(3237): note: see reference to function template instantiation 'void nlohmann::adl_serializer<T,void>::from_json<const nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>&,T>(BasicJsonType,ValueType &) noexcept(false)' being compiled
    1>        with
    1>        [
    1>            T=std::array<glm::vec2,2>,
    1>            BasicJsonType=const nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer> &,
    1>            ValueType=std::array<glm::vec2,2>
    1>        ]
    1>c:\...\thirdparty\json\nlohmann_json.hpp(3237): note: see reference to function template instantiation 'void nlohmann::adl_serializer<T,void>::from_json<const nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>&,T>(BasicJsonType,ValueType &) noexcept(false)' being compiled
    1>        with
    1>        [
    1>            T=std::array<glm::vec2,2>,
    1>            BasicJsonType=const nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer> &,
    1>            ValueType=std::array<glm::vec2,2>
    1>        ]
    1>{cpp file being compiled}(45): note: see reference to function template instantiation 'ValueType nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::get<std::array<glm::vec2,2>,std::array<glm::vec2,2>,0>(void) noexcept(false) const' being compiled
    1>        with
    1>        [
    1>            ValueType=std::array<glm::vec2,2>
    1>        ]
    1>{cpp file being compiled}(45): note: see reference to function template instantiation 'ValueType nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::get<std::array<glm::vec2,2>,std::array<glm::vec2,2>,0>(void) noexcept(false) const' being compiled
    1>        with
    1>        [
    1>            ValueType=std::array<glm::vec2,2>
    1>        ]
    1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.13.26128\include\xlocale(446): note: see reference to class template instantiation 'std::basic_string_view<char,std::char_traits<char>>' being compiled
    
  3. theodelrieu commented on May 2, 2018

    @theodelrieu
    Contributor

    Could you reproduce the issue with a minimal code snippet?

  4. Yitzi2 commented on May 2, 2018

    @Yitzi2
    Author

    Here's my minimal code snippet:

    #include <nlohmann_json.hpp>

    nlohmann::json exampleJSON;
    auto exampleOutput = exampleJSON["test"].get<std::array<float, 2>>();

    However, I did a bit of experimenting, and found that the version of the library I'm using is a bit out-of-date (or perhaps modified), and this is causing the conflict; if I replace it with the version from master, it works fine. Sorry, I didn't realize the one I was using wasn't up-to-date.

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

    state: needs more infothe author of the issue needs to provide more details

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions