[英]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
function 可以讀取帶有數字的 arrays 到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 數組中獲取值的位置,我收到錯誤“沒有匹配的成員 function 用於調用 'Get' ”[1]。 出於同樣的原因,使用rapidjson::GenericValue
而不是rapidjson::Value
也不起作用。 rapidjson::GenericArray<false, ValueType>
在我運行ReadArray
[2] 的位置引發了“ No matching function for call to 'ReadTensor' ”。
我該如何解決這個問題? 如何將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);
}
缺少的鏈接是使用it->template Get<ValueType>()
而不是it->Get()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.