我在序列化 astd::string
时遇到boost::serialization
了一些问题text_oarchive
。AFAICT,我有两段相同的代码在两个不同的程序中表现不同。
这是我认为行为正确的程序:
#include <iostream>
#include <string>
#include <sstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
template <typename T>
void serialize_deserialize(const T & src, T & dst)
{
std::string serialized_data_str;
std::cout << "original data: " << src << std::endl;
std::ostringstream archive_ostream;
boost::archive::text_oarchive oarchive(archive_ostream);
oarchive << src;
serialized_data_str = archive_ostream.str();
std::cout << "serialized data: " << serialized_data_str << std::endl;
std::istringstream archive_istream(serialized_data_str);
boost::archive::text_iarchive iarchive(archive_istream);
iarchive >> dst;
}
int main()
{
std::string archived_data_str = "abcd";
std::string restored_data_str;
serialize_deserialize<std::string>(archived_data_str, restored_data_str);
std::cout << "restored data: " << restored_data_str << std::endl;
return 0;
}
这是它的输出:
original data: abcd
serialized data: 22 serialization::archive 10 4 abcd
restored data: abcd
(你可以编译它g++ boost-serialization-string.cpp -o boost-serialization-string -lboost_serialization
:)
另一方面,这个是我正在编写的程序的摘录(源自boost_asio/example/serialization/connection.hpp),它序列化std::string
数据,以十六进制表示形式转换每个字符:
/// Asynchronously write a data structure to the socket.
template <typename T, typename Handler>
void async_write(const T& t, Handler handler)
{
// Serialize the data first so we know how large it is.
std::cout << "original data: " << t << std::endl;
std::ostringstream archive_stream;
boost::archive::text_oarchive archive(archive_stream);
archive << t;
outbound_data_ = archive_stream.str();
std::cout << "serialized data: " << outbound_data_ << std::endl;
[...]
这是其输出的摘录:
original data: abcd
serialized data: 22 serialization::archive 10 5 97 98 99 100 0
版本(10
)是一样的,对吧?所以这应该证明我在两个程序中都使用了相同的序列化库。
但是,我真的无法弄清楚这里发生了什么。我几乎整整一个工作日都在尝试解决这个难题,但我没有想法。
对于可能想要重现此结果的任何人,下载Boost 序列化示例就足够了,添加以下行
connection_.async_write("abcd", boost::bind(&client::handle_write, this, boost::asio::placeholders::error));
在第 50 行client.cpp
,在 client.cpp 中添加以下成员函数
/// Handle completion of a write operation.
void handle_write(const boost::system::error_code& e)
{
// Nothing to do. The socket will be closed automatically when the last
// reference to the connection object goes away.
}
添加这个cout
:
std::cout << "serialized data: " << outbound_data_ << std::endl;
在connection.hpp:59
并编译:
g++ -O0 -g3 client.cpp -o client -lboost_serialization -lboost_system
g++ -O0 -g3 server.cpp -o server -lboost_serialization -lboost_system
我正在使用g++ 4.8.1
under Ubuntu 13.04 64bit
withBoost 1.53
任何帮助将不胜感激。
Ps 我发布这个是因为std::string
s 的反序列化根本不起作用!:)