我试图读取压缩的 json 并遇到类型转换问题,这是代码
boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
std::istringstream iss(std::ios::binary);
iss.rdbuf()->pubsetbuf(buf, len);
iss.imbue( std::locale("ru_RU.CP1251") );
in.push( boost::iostreams::zlib_decompressor() );
in.push( iss );
boost::property_tree::ptree pt;
boost::property_tree::json_parser::read_json(in, pt); // <-- Compile error
编译器说:
src/ABPacking.cpp:48: error: no matching function for call to ‘read_json(boost::iostreams::filtering_streambuf, std::allocator, boost::iostreams::public_>&, boost::property_tree::ptree&)’
问题是如何将 filtering_streambuf 传递给 read_json 而不进行不必要的数据复制?
最佳答案
read_json
需要文件名或包含 JSON 内容的流。您正在尝试传递流缓冲区,但它不知道如何处理它。
作为解决方案,只需将流缓冲区传递给使用它的 istream
并将其传递给 read_json
:
std::istream input(&in_buf);
read_json(input, pt);
关于c++ - boost property_tree::json_parser::read_json 和 iostreams::filtering_streambuf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12542399/