将提升属性树 (ptree) 序列化为向量的最快方法是什么

What is the fastest way to serialize a boost property tree (ptree) to a vector

本文关键字:向量 是什么 方法 序列化 属性 ptree      更新时间:2023-10-16

我有这个代码:

std::vector<uint8_t> getWriteBuffer()
{
boost::property_tree::ptree jsonTree=getJson();  //This function returns a json  in a ptree
// I have this code, but is there any faster way to do this?
std::ostringstream jsonStream;
boost::property_tree::write_json(jsonStream, jsonTree);
std::string jsonString = jsonStream.str();
std::vector<uint8_t> output(jsonString.begin(), jsonString.end());
return output;
}

作为代码,我可以通过将 Ptree 写入字符串流,然后将其转换为字符串,然后将其复制到缓冲区来做到这一点。

有没有更快的方法可以做到这一点?

我可能只是写一个流,该流位于back_inserter_device周围的流缓冲区上:https://www.boost.org/doc/libs/1_67_0/libs/iostreams/doc/classes/back_inserter.html

您还可以将其与 Boost 序列化结合使用,请参阅:

  • 提升::p roperty_tree::p树序列化和反序列化

在这种情况下,使用 binary_oarchive 并使用no_header存档标志将减少数据量。