问题
There are some message structs. Each one can be serialized to a string and de-serialized from a string. For the serialization part, I use the overload operator <<. But for the de-serialization part, I cannot think of a proper way to do so. So I use a class to parse the string. Recently, I came across boost serialization. I don't know if it can serve this purpose or there is any better idea.
struct S
{
int32_t type;
double a;
int32_t b;
bool c;
std::string d;
friend std::ostream& operator<< (std::ostream& os, const S& s)
{
os << "{field1" << "=" << s.a << "|";
os << "field2" << "=" << s.b << "|";
os << "field3" << "=" << s.c << "|";
os << "field4" << "=" << s.d << "}";
return os;
}
};
EDIT: So, I choose to use xml archive. However, I have a another issue. Since there are several type of message which is classified by the field msgtype. When deserialization, How to specify which object is going to deserialize to? Do I need to manually search msgtype field?
template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
using boost::serialization::make_nvp;
ar & make_nvp("msgtype", type);
ar & make_nvp("field1", a);
ar & make_nvp("field2", b);
ar & make_nvp("field3", c);
ar & make_nvp("field4", d);
}
回答1:
Use Boost Serialization:
http://www.boost.org/doc/libs/1_52_0/libs/serialization/doc/index.html
With this library it will pretty much take care of everything for you. For example you could just add this function to your struct:
void serialize(Archive & ar, const unsigned int version)
{
ar & a;
ar & b;
ar & c;
ar & d;
}
Then you will be able to serialize and de-serialize by doing this:
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << g;
and this:
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> newg;
assuming g
and newg
are your struct.
You can then also change to binary_archive
or other to conserve space, or text_archive
to conserve readability.
EDIT: For your edit, boost serialization will handle de serialization for you. As long as you serialised to a archive you just need to do the opposite from that archive to the type that the archive was created from and boost will put everything back in the right places.
I am not familiar with NVP and XML stuff in boost so if it is different than i am sorry.
回答2:
boost::serialization (as far as I remember) supports few representations: binary, text and XML. And it wouldn't be hard (AFAIK) to extend it to serialize to/from anything else (JSON for example (maybe it's already done, I don't know)).
If you'd like to reinvent the wheel you can follow design principles of boost::serialization -- it's pretty clear end (relatively) easy to reimplement in a simplified way (w/o support for linked objects, which is not needed for most cases). But looking at your code I think you'd better to use smth else (already tested) than your own serialization...
Sorry, but your design is far from good...
来源:https://stackoverflow.com/questions/14431961/struct-serialization