Boost 库 write_json 在末尾添加额外的新行
Boost library write_json put extra new line at the end
我正在向 http://www.cochoy.fr/boost-property-tree/ 学习。
我试图将它保存在一个字符串中,而不是 write_json 到标准输出。
std::stringstream ss;
boost::property_tree::json_parser::write_json(ss, oroot, false);
std::cout <<" begin json string" << std::endl;
std::cout << ss.str() << std::endl;
std::cout << "after json string" << std::endl;
输出:
begin json string
{"height":"320","some":{"complex":{"path":"bonjour"}},"animals":{"rabbit":"white","dog":"brown","cat":"grey"},"fish":"blue","fish":"yellow","fruits":["apple","raspberry","orange"],"matrix":[["1","2","3"],["4","5","6"],["7","8","9"]]}
after json string
根据上面的输出,末尾有一个新的空行。如何摆脱新线?因为新行不是有效的 JSON 字符串。
JSON RFC-7159 but it is defined as part of the POSIX standard for a line中没有明确提到换行符。
如果您对换行符的来源感兴趣,可以查看 write_json_internal source code,我们可以看到在方法末尾附近有一个 stream << std::endl;
。请注意 ...::write_json 引用 write_json_internal.
// Write ptree to json stream
template<class Ptree>
void write_json_internal(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
const Ptree &pt,
const std::string &filename,
bool pretty)
{
if (!verify_json(pt, 0))
BOOST_PROPERTY_TREE_THROW(json_parser_error("ptree contains data that cannot be represented in JSON format", filename, 0));
write_json_helper(stream, pt, 0, pretty);
stream << std::endl;
if (!stream.good())
BOOST_PROPERTY_TREE_THROW(json_parser_error("write error", filename, 0));
}
我正在向 http://www.cochoy.fr/boost-property-tree/ 学习。 我试图将它保存在一个字符串中,而不是 write_json 到标准输出。
std::stringstream ss;
boost::property_tree::json_parser::write_json(ss, oroot, false);
std::cout <<" begin json string" << std::endl;
std::cout << ss.str() << std::endl;
std::cout << "after json string" << std::endl;
输出:
begin json string
{"height":"320","some":{"complex":{"path":"bonjour"}},"animals":{"rabbit":"white","dog":"brown","cat":"grey"},"fish":"blue","fish":"yellow","fruits":["apple","raspberry","orange"],"matrix":[["1","2","3"],["4","5","6"],["7","8","9"]]}
after json string
根据上面的输出,末尾有一个新的空行。如何摆脱新线?因为新行不是有效的 JSON 字符串。
JSON RFC-7159 but it is defined as part of the POSIX standard for a line中没有明确提到换行符。
如果您对换行符的来源感兴趣,可以查看 write_json_internal source code,我们可以看到在方法末尾附近有一个 stream << std::endl;
。请注意 ...::write_json 引用 write_json_internal.
// Write ptree to json stream
template<class Ptree>
void write_json_internal(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
const Ptree &pt,
const std::string &filename,
bool pretty)
{
if (!verify_json(pt, 0))
BOOST_PROPERTY_TREE_THROW(json_parser_error("ptree contains data that cannot be represented in JSON format", filename, 0));
write_json_helper(stream, pt, 0, pretty);
stream << std::endl;
if (!stream.good())
BOOST_PROPERTY_TREE_THROW(json_parser_error("write error", filename, 0));
}