RapidJSON/C++:从 JSON 数组创建 std::vector<?>

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

我想用 RapidJSON 阅读 JSON 文档。我的 JSON 文档包含一个多维矩阵并具有以下形式:

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

我想编写一个 ReadArray 函数,可以将带有数字的数组(例如,本例中的形状和数据)读入 std::vector。向量的类型对于形状应该是 int 并且对于数据应该是适当的 (float/int)。

我试过的是:

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'
    }
}

我会这样称呼它:

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);

在我尝试从 JSON 数组中获取值的位置,出现错误“没有匹配的成员函数来调用 'Get'”[1]。出于同样的原因,使用 rapidjson::GenericValue 而不是 rapidjson::Value 也不起作用。 rapidjson::GenericArray<false, ValueType> 而不是在我 运行 ReadArray [2] 的位置引发“ 没有匹配函数调用 'ReadTensor'”。 =28=]

我该如何解决这个问题?我怎样才能将 rapidjson::Value 转换为数组类型,或者我应该在这里尝试完全不同的东西?

[1] 使用 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] 使用 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);
                               ^

感谢@273K,我可以让它按如下方式工作:

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);
}

缺少的 link 是使用 it->template Get<ValueType>() 而不是 it->Get()