c++templatesgenericsstdvectorrapidjson

RapidJSON/C++: Create std::vector<?> from JSON array


I want to read a JSON document with RapidJSON. My JSON document contains a multi-dimensional matrix and has the following form:

{
  "shape": [1, 120, 42],
  "type": "float32",
  "data": [0.123, 0.234, ..., 0.345]
}

I would like to write a ReadArray function that can read the arrays with numbers (e.g., shape and data in this example) into a std::vector. The type of the vector should be int for the shape and appropriate (float/int) for the data.

What I have tried is:

template <typename ValueType>
void ReadArray(rapidjson::Value& jsonArray, std::vector<ValueType> output) {
    for (auto it = jsonTensor.Begin(); it != jsonTensor.End(); it++) {
        output.emplace_back(it->Get()); // error: No matching member function for call to 'Get'
    }
}

I will call this as follows:

std::string dataPath("/path/to/data.json"); // data.json content see above

std::ifstream dataInputStream(dataPath);
rapidjson::IStreamWrapper dataInputStreamWrapper(dataInputStream);
rapidjson::Document jsonTensor;
jsonTensor.ParseStream(dataInputStreamWrapper);

auto jsonShape = jsonTensor["shape"].GetArray();
std::vector<int64_t> shape;
ReadArray(jsonShape, shape);

auto jsonData = jsonTensor["data"].GetArray();
std::vector<float> data;
ReadArray(jsonData, data);

At the location where I try to get the value from the JSON array, I get the error "No matching member function for call to 'Get'" [1]. Using rapidjson::GenericValue instead of rapidjson::Value is also not working for the same reason. rapidjson::GenericArray<false, ValueType> instead raises "No matching function for call to 'ReadTensor'" in the location where I run ReadArray [2].

How could I solve this issue? How could I convert the rapidjson::Value to an array type or should I try something completely else here?

[1] Error message when using Value:

/home/user/test/main.cpp: In function ‘void ReadArray(rapidjson::Value&, std::vector<ValueType>)’:
/home/user/test/main.cpp:17:37: error: no matching function for call to ‘rapidjson::GenericValue<rapidjson::UTF8<> >::Get()’
         output.emplace_back(it->Get());
                                     ^
In file included from /home/user/test/main.cpp:7:0:
/home/user/.local/include/rapidjson/document.h:1912:7: note: candidate: template<class T> T rapidjson::GenericValue<Encoding, Allocator>::Get() const [with T = T; Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>]
     T Get() const { return internal::TypeHelper<ValueType, T>::Get(*this); }
       ^
/home/user/.local/include/rapidjson/document.h:1912:7: note:   template argument deduction/substitution failed:
/home/user/test/main.cpp:17:37: note:   couldn't deduce template parameter ‘T’
         output.emplace_back(it->Get());
                                     ^
In file included from /home/user/test/main.cpp:7:0:
/home/user/.local/include/rapidjson/document.h:1915:7: note: candidate: template<class T> T rapidjson::GenericValue<Encoding, Allocator>::Get() [with T = T; Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>]
     T Get() { return internal::TypeHelper<ValueType, T>::Get(*this); }
       ^
/home/user/.local/include/rapidjson/document.h:1915:7: note:   template argument deduction/substitution failed:
/home/user/test/main.cpp:17:37: note:   couldn't deduce template parameter ‘T’
         output.emplace_back(it->Get());
                                     ^

[2] Error message when using GenericArray:

/home/user/test/main.cpp: In function ‘int main(int, char**)’:
/home/user/test/main.cpp:89:31: error: no matching function for call to ‘ReadArray(rapidjson::GenericArray<false, rapidjson::GenericValue<rapidjson::UTF8<> > >&, std::vector<long int>&)’
     ReadArray(jsonShape, shape);
                               ^
/home/user/test/main.cpp:15:6: note: candidate: template<class ValueType> void ReadArray(rapidjson::GenericArray<false, ValueType>&, std::vector<ValueType>)
 void ReadArray(rapidjson::GenericArray<false, ValueType>& jsonTensor, std::vector<ValueType> output) {
      ^
/home/user/test/main.cpp:15:6: note:   template argument deduction/substitution failed:
/home/user/test/main.cpp:89:31: note:   deduced conflicting types for parameter ‘ValueType’ (‘rapidjson::GenericValue<rapidjson::UTF8<> >’ and ‘long int’)
     ReadArray(jsonShape, shape);
                               ^

Solution

  • Thanks to @273K, I could get it to work as follows:

    template <typename ValueType>
    void ReadArray(rapidjson::Value jsonArray, std::vector<ValueType>& output) {
        for (auto it = jsonArray.Begin(); it != jsonArray.End(); it++) {
            auto value = it->template Get<ValueType>();
            output.emplace_back(value);
        }
    }
    
    int main(int argc, char* argv[]) {
        std::ifstream dataInputStream(dataPath);
        rapidjson::IStreamWrapper dataInputStreamWrapper(dataInputStream);
        rapidjson::Document jsonTensor;
        jsonTensor.ParseStream(dataInputStreamWrapper);
    
        auto jsonShape = jsonTensor["shape"].GetArray();
        std::vector<int64_t> shape;
        ReadArray(jsonShape, shape);
    }
    

    The missing link was to use it->template Get<ValueType>() instead of it->Get().