c++ - boost property_tree::json_parser::read_json 和 iostreams::filtering_streambuf

标签 c++ json boost stream

我试图读取压缩的 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/

相关文章:

c++ - 从 Unix 迁移到 Linux 时 Makefile Include 指令

jquery - 如何从 HTML 表单创建分层 JSON 字符串

javascript - 使用所选的选择选项使用 angularjs 解析 JSON

c++ - boost 文件系统 : recursive_directory_iterator constructor causes SIGTRAPS and debug problems

python - 如何使用 boost 元组返回两个 vector

c++ - 访问类成员时的性能

python - 扩展 python - swig,而不是 swig 或 Cython

c++ - C++中的复制和删除

javascript - 基于日期 YYYY.MM.DD 的 JSON 查找样式

c++ - boost 的进程间段管理器分配器本身可以与其他进程共享吗?